geopm  3.1.1.dev272+gdfb40a8d
GEOPM - Global Extensible Open Power Manager
Helper.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 - 2024 Intel Corporation
3  * SPDX-License-Identifier: BSD-3-Clause
4  */
5 
6 #ifndef HELPER_HPP_INCLUDE
7 #define HELPER_HPP_INCLUDE
8 
9 #include <sched.h>
10 #include <stdint.h>
11 
12 #include <functional>
13 #include <memory>
14 #include <set>
15 #include <string>
16 #include <utility>
17 #include <vector>
18 
19 #include "geopm_public.h"
20 
21 namespace geopm
22 {
23  class PlatformIO;
27  template <class Type, class ...Args>
28  std::unique_ptr<Type> make_unique(Args &&...args)
29  {
30  return std::unique_ptr<Type>(new Type(std::forward<Args>(args)...));
31  }
32 
36  std::string GEOPM_PUBLIC
37  read_file(const std::string &path);
38 
47  double GEOPM_PUBLIC
48  read_double_from_file(const std::string &path, const std::string &expected_units);
49 
54  void GEOPM_PUBLIC
55  write_file(const std::string &path, const std::string &contents);
56 
62  std::vector<std::string> GEOPM_PUBLIC
63  string_split(const std::string &str, const std::string &delim);
64 
69  std::string GEOPM_PUBLIC
70  string_join(const std::vector<std::string> &string_list, const std::string &delim);
71 
73  std::string GEOPM_PUBLIC
74  hostname(void);
75 
78  std::vector<std::string> GEOPM_PUBLIC
79  list_directory_files(const std::string &path);
80 
82  bool GEOPM_PUBLIC
83  string_begins_with(const std::string &str, const std::string &key);
84 
86  bool GEOPM_PUBLIC
87  string_ends_with(std::string str, std::string key);
88 
94  };
96  std::function<std::string(double)> GEOPM_PUBLIC
97  string_format_type_to_function(int format_type);
99  std::function<std::string(double)> GEOPM_PUBLIC
100  string_format_name_to_function(const std::string &format_name);
102  int GEOPM_PUBLIC
103  string_format_function_to_type(std::function<std::string(double)> format_function);
109  std::string GEOPM_PUBLIC
110  string_format_double(double signal);
111 
117  std::string GEOPM_PUBLIC
118  string_format_float(double signal);
119 
125  std::string GEOPM_PUBLIC
126  string_format_integer(double signal);
127 
134  std::string GEOPM_PUBLIC
135  string_format_hex(double signal);
136 
142  std::string GEOPM_PUBLIC
143  string_format_raw64(double signal);
144 
148  static constexpr int hardware_destructive_interference_size = 64;
149 
153  std::string GEOPM_PUBLIC
154  get_env(const std::string &name);
155 
169  int GEOPM_PUBLIC
170  verbosity_level(void);
171 
175  unsigned int GEOPM_PUBLIC
176  pid_to_uid(const int pid);
177 
181  unsigned int GEOPM_PUBLIC
182  pid_to_gid(const int pid);
183 
189  std::unique_ptr<cpu_set_t, std::function<void(cpu_set_t *)> > GEOPM_PUBLIC
190  make_cpu_set(int num_cpu, const std::set<int> &cpu_enabled);
191 
194  bool GEOPM_PUBLIC
195  has_cap_sys_admin(void);
196 
200  bool GEOPM_PUBLIC
201  has_cap_sys_admin(int pid);
202 
204  {
205  public:
206  DeprecationWarning() = delete;
207  DeprecationWarning(const std::string &function, const std::string &message);
208  virtual ~DeprecationWarning() = default;
209  };
210 
213  std::string GEOPM_PUBLIC
214  read_symlink_target(const std::string &symlink_path);
215 
219  void GEOPM_PUBLIC
221 
222 }
223 
224 #endif
Definition: Helper.hpp:204
virtual ~DeprecationWarning()=default
Class which is a collection of all valid control and signal objects for a platform.
Definition: PlatformIO.hpp:26
#define GEOPM_PUBLIC
Definition: geopm_public.h:10
Definition: Agg.cpp:20
void enable_fixed_counters(PlatformIO &pio)
Definition: Helper.cpp:435
bool string_ends_with(std::string str, std::string key)
Returns whether one string ends with another.
Definition: Helper.cpp:168
std::function< std::string(double)> string_format_type_to_function(int format_type)
Convert a format type enum string_format_e to a format function.
Definition: Helper.cpp:219
std::string string_join(const std::vector< std::string > &list, const std::string &delim)
Joins a vector of strings together with a delimiter.
Definition: Helper.cpp:118
std::vector< std::string > string_split(const std::string &str, const std::string &delim)
Splits a string according to a delimiter.
Definition: Helper.cpp:96
std::string get_env(const std::string &name)
Read an environment variable.
Definition: Helper.cpp:268
int string_format_function_to_type(std::function< std::string(double)> format_function)
Convert a format function to a format type enum string_format_e.
Definition: Helper.cpp:251
std::unique_ptr< Type > make_unique(Args &&...args)
Implementation of std::make_unique (C++14) for C++11. Note that this version will only work for non-a...
Definition: Helper.hpp:28
std::string read_symlink_target(const std::string &symlink_path)
Definition: Helper.cpp:408
std::string hostname(void)
Returns the current hostname as a string.
Definition: Helper.cpp:132
std::string string_format_raw64(double signal)
Format a string to represent the raw memory supporting a signal as an unsigned hexadecimal integer.
Definition: Helper.cpp:211
std::string string_format_double(double signal)
Format a string to best represent a signal encoding a double precision floating point number.
Definition: Helper.cpp:175
void write_file(const std::string &path, const std::string &contents)
Writes a string to a file. This will replace the file if it exists or create it if it does not exist.
Definition: Helper.cpp:84
std::string read_file(const std::string &path)
Reads the specified file and returns the contents in a string.
Definition: Helper.cpp:41
unsigned int pid_to_gid(const int pid)
Query for the group id associated with the process id.
Definition: Helper.cpp:308
std::string string_format_integer(double signal)
Format a string to best represent a signal encoding a decimal integer.
Definition: Helper.cpp:189
std::vector< std::string > list_directory_files(const std::string &path)
List all files in the given directory.
Definition: Helper.cpp:143
std::unique_ptr< cpu_set_t, std::function< void(cpu_set_t *)> > make_cpu_set(int num_cpu, const std::set< int > &cpu_enabled)
Wrapper around CPU_ALLOC and CPU_FREE.
Definition: Helper.cpp:320
bool has_cap_sys_admin(void)
Check if the caller has effective capability CAP_SYS_ADMIN.
Definition: Helper.cpp:380
std::string string_format_float(double signal)
Format a string to best represent a signal encoding a single precision floating point number.
Definition: Helper.cpp:182
string_format_e
Definition: Helper.hpp:89
@ STRING_FORMAT_RAW64
Definition: Helper.hpp:93
@ STRING_FORMAT_DOUBLE
Definition: Helper.hpp:90
@ STRING_FORMAT_INTEGER
Definition: Helper.hpp:91
@ STRING_FORMAT_HEX
Definition: Helper.hpp:92
std::string string_format_hex(double signal)
Format a string to best represent a signal encoding an unsigned hexadecimal integer.
Definition: Helper.cpp:201
int verbosity_level(void)
Query environment for verbosity level.
Definition: Helper.cpp:278
std::function< std::string(double)> string_format_name_to_function(const std::string &format_name)
Convert a format function to a format name to a format function.
Definition: Helper.cpp:235
unsigned int pid_to_uid(const int pid)
Query for the user id associated with the process id.
Definition: Helper.cpp:297
bool string_begins_with(const std::string &str, const std::string &key)
Returns whether one string begins with another.
Definition: Helper.cpp:163
double read_double_from_file(const std::string &path, const std::string &expected_units)
Read a file and return a double read from the file.
Definition: Helper.cpp:60