geopm  3.1.0
GEOPM - Global Extensible Open Power Manager
All Classes Namespaces Files Functions Variables Enumerations Enumerator Friends Macros Pages
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 {
26  template <class Type, class ...Args>
27  std::unique_ptr<Type> make_unique(Args &&...args)
28  {
29  return std::unique_ptr<Type>(new Type(std::forward<Args>(args)...));
30  }
31 
35  std::string GEOPM_PUBLIC
36  read_file(const std::string &path);
37 
46  double GEOPM_PUBLIC
47  read_double_from_file(const std::string &path, const std::string &expected_units);
48 
53  void GEOPM_PUBLIC
54  write_file(const std::string &path, const std::string &contents);
55 
61  std::vector<std::string> GEOPM_PUBLIC
62  string_split(const std::string &str, const std::string &delim);
63 
68  std::string GEOPM_PUBLIC
69  string_join(const std::vector<std::string> &string_list, const std::string &delim);
70 
72  std::string GEOPM_PUBLIC
73  hostname(void);
74 
77  std::vector<std::string> GEOPM_PUBLIC
78  list_directory_files(const std::string &path);
79 
81  bool GEOPM_PUBLIC
82  string_begins_with(const std::string &str, const std::string &key);
83 
85  bool GEOPM_PUBLIC
86  string_ends_with(std::string str, std::string key);
87 
93  };
95  std::function<std::string(double)> GEOPM_PUBLIC
96  string_format_type_to_function(int format_type);
98  std::function<std::string(double)> GEOPM_PUBLIC
99  string_format_name_to_function(const std::string &format_name);
101  int GEOPM_PUBLIC
102  string_format_function_to_type(std::function<std::string(double)> format_function);
108  std::string GEOPM_PUBLIC
109  string_format_double(double signal);
110 
116  std::string GEOPM_PUBLIC
117  string_format_float(double signal);
118 
124  std::string GEOPM_PUBLIC
125  string_format_integer(double signal);
126 
133  std::string GEOPM_PUBLIC
134  string_format_hex(double signal);
135 
141  std::string GEOPM_PUBLIC
142  string_format_raw64(double signal);
143 
147  static constexpr int hardware_destructive_interference_size = 64;
148 
152  std::string GEOPM_PUBLIC
153  get_env(const std::string &name);
154 
158  unsigned int GEOPM_PUBLIC
159  pid_to_uid(const int pid);
160 
164  unsigned int GEOPM_PUBLIC
165  pid_to_gid(const int pid);
166 
172  std::unique_ptr<cpu_set_t, std::function<void(cpu_set_t *)> > GEOPM_PUBLIC
173  make_cpu_set(int num_cpu, const std::set<int> &cpu_enabled);
174 
177  bool GEOPM_PUBLIC
178  has_cap_sys_admin(void);
179 
183  bool GEOPM_PUBLIC
184  has_cap_sys_admin(int pid);
185 
187  {
188  public:
189  DeprecationWarning() = delete;
190  DeprecationWarning(const std::string &function, const std::string &message);
191  virtual ~DeprecationWarning() = default;
192  };
193 }
194 
195 #endif
Definition: Helper.hpp:187
virtual ~DeprecationWarning()=default
#define GEOPM_PUBLIC
Definition: geopm_public.h:10
Definition: Agg.cpp:20
bool string_ends_with(std::string str, std::string key)
Returns whether one string ends with another.
Definition: Helper.cpp:165
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:216
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:115
std::vector< std::string > string_split(const std::string &str, const std::string &delim)
Splits a string according to a delimiter.
Definition: Helper.cpp:93
std::string get_env(const std::string &name)
Read an environment variable.
Definition: Helper.cpp:265
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:248
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:27
std::string hostname(void)
Returns the current hostname as a string.
Definition: Helper.cpp:129
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:208
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:172
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:81
std::string read_file(const std::string &path)
Reads the specified file and returns the contents in a string.
Definition: Helper.cpp:38
unsigned int pid_to_gid(const int pid)
Query for the group id associated with the process id.
Definition: Helper.cpp:286
std::string string_format_integer(double signal)
Format a string to best represent a signal encoding a decimal integer.
Definition: Helper.cpp:186
std::vector< std::string > list_directory_files(const std::string &path)
List all files in the given directory.
Definition: Helper.cpp:140
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:298
bool has_cap_sys_admin(void)
Check if the caller has effective capability CAP_SYS_ADMIN.
Definition: Helper.cpp:358
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:179
string_format_e
Definition: Helper.hpp:88
@ STRING_FORMAT_RAW64
Definition: Helper.hpp:92
@ STRING_FORMAT_DOUBLE
Definition: Helper.hpp:89
@ STRING_FORMAT_INTEGER
Definition: Helper.hpp:90
@ STRING_FORMAT_HEX
Definition: Helper.hpp:91
std::string string_format_hex(double signal)
Format a string to best represent a signal encoding an unsigned hexadecimal integer.
Definition: Helper.cpp:198
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:232
unsigned int pid_to_uid(const int pid)
Query for the user id associated with the process id.
Definition: Helper.cpp:275
bool string_begins_with(const std::string &str, const std::string &key)
Returns whether one string begins with another.
Definition: Helper.cpp:160
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:57