ArmNN
 24.02
SystemPropertiesUtils.hpp
Go to the documentation of this file.
1 //
2 // Copyright © 2022 Arm Ltd and Contributors. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
6 #pragma once
7 
8 #include <stdio.h>
9 #include <string>
10 #include <iostream>
11 #include <sys/system_properties.h>
12 #include <log/log.h>
13 
14 namespace {
15 template<typename T>
16 struct ConvStringTo;
17 
18 template<>
19 struct ConvStringTo<float>
20 {
21  static float Func(std::string s) { return std::stof(s); }
22 };
23 
24 template<>
25 struct ConvStringTo<int>
26 {
27  static int Func(std::string s) { return std::stoi(s); }
28 };
29 
30 template<>
31 struct ConvStringTo<bool>
32 {
33  static bool Func(std::string s) { return !!std::stoi(s); }
34 };
35 
36 template<typename T>
37 void GetCapabilitiesProperties([[maybe_unused]]void* cookie,
38  [[maybe_unused]]const char *name,
39  [[maybe_unused]]const char *value,
40  [[maybe_unused]]uint32_t serial)
41 {
42  T &prop = *reinterpret_cast<T*>(cookie);
43  prop = ConvStringTo<T>::Func(std::string(value));
44 }
45 
46 template<typename T>
47 T ParseSystemProperty(const char* name, T defaultValue)
48 {
49  try
50  {
51  const prop_info *pInfo = __system_property_find(name);
52  if (!pInfo)
53  {
54  ALOGW("ArmnnDriver::ParseSystemProperty(): Could not find property [%s].", name);
55  } else
56  {
57  T property;
58  __system_property_read_callback(pInfo, &GetCapabilitiesProperties<T>, &property);
59  std::stringstream messageBuilder;
60  messageBuilder << "ArmnnDriver::ParseSystemProperty(): Setting [" << name << "]=[" << property << "].";
61  ALOGD("%s", messageBuilder.str().c_str());
62  return property;
63  }
64  }
65  catch(const std::invalid_argument& e)
66  {
67  ALOGD("ArmnnDriver::ParseSystemProperty(): Property [%s] has invalid data type.", name);
68  }
69  catch(const std::out_of_range& e)
70  {
71  ALOGD("ArmnnDriver::ParseSystemProperty(): Property [%s] out of range for the data type.", name);
72  }
73  catch (...)
74  {
75  ALOGD("ArmnnDriver::ParseSystemProperty(): Unexpected exception reading system "
76  "property [%s].", name);
77  }
78 
79  std::stringstream messageBuilder;
80  messageBuilder << "ArmnnDriver::ParseSystemProperty(): Falling back to default value [" << defaultValue << "]";
81  ALOGD("%s", messageBuilder.str().c_str());
82  return defaultValue;
83 }
84 } //namespace