aboutsummaryrefslogtreecommitdiff
path: root/src/powercap-common.h
blob: 6e31252ee084d3ad2722590317c3a0b2b56bd23b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * SPDX-License-Identifier: BSD-3-Clause
 *
 * Common utilities, like logging.
 *
 * @author Connor Imes
 * @date 2017-05-09
 */
#ifndef _POWERCAP_COMMON_H_
#define _POWERCAP_COMMON_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <limits.h>
#include <stdio.h>
/* Main powercap header only used for enums */
#include "powercap.h"

#pragma GCC visibility push(hidden)

typedef enum powercap_loglevel {
  DEBUG = 0,
  INFO,
  WARN,
  ERROR,
  OFF,
} powercap_loglevel;

#ifndef POWERCAP_LOG_LEVEL
  #define POWERCAP_LOG_LEVEL WARN
#endif

#define TO_FILE(severity) (severity) >= WARN ? stderr : stdout

#define TO_LOG_PREFIX(severity) \
  (severity) == DEBUG ? "[DEBUG]" : \
  (severity) == INFO  ? "[INFO] " : \
  (severity) == WARN  ? "[WARN] " : \
                        "[ERROR]"

#define LOG(severity, ...) \
  do { if ((severity) >= POWERCAP_LOG_LEVEL) { \
      fprintf(TO_FILE((severity)), "%s [powercap] ", TO_LOG_PREFIX((severity))); \
      fprintf(TO_FILE((severity)), __VA_ARGS__); \
    } } while (0)

/* PATH_MAX should be defined in limits.h */
#ifndef PATH_MAX
  #pragma message("Warning: PATH_MAX was not defined")
  #define PATH_MAX 4096
#endif

/* buf must not be NULL and size >= 1 */
ssize_t read_string_safe(int fd, char* buf, size_t size);

/* Return number of bytes read (including terminating NULL char) on success, negative error code on failure */
ssize_t read_string(int fd, char* buf, size_t size);

/* Return 0 on success, negative error code on failure */
int read_u64(int fd, uint64_t* val);

/* Return 0 on success, negative error code on failure */
int write_u64(int fd, uint64_t val);

/* Simple names only, trying to look outside the powercap directory is not allowed */
int is_valid_control_type(const char* control_type);

/*
 * Return is like snprintf, except if the output was truncated due to the size limit, the return value is still > size,
 * but not necessarily the number of characters (excluding the terminating null byte) which would have been written to
 * the final string if enough space had been available.
 */
int snprintf_base_path(char* buf, size_t size, const char* control_type, const uint32_t* zones, uint32_t depth);

/* Return is like snprintf */
int snprintf_control_type_file(char* buf, size_t size, powercap_control_type_file type);

/* Return is like snprintf */
int snprintf_zone_file(char* buf, size_t size, powercap_zone_file type);

/* Return is like snprintf */
int snprintf_constraint_file(char* buf, size_t size, powercap_constraint_file type, uint32_t constraint);

/* Return is like snprintf_base_path */
int snprintf_control_type_file_path(char* path, size_t size, const char* control_type, powercap_control_type_file type);

/* Return is like snprintf_base_path */
int snprintf_zone_file_path(char* path, size_t size, const char* control_type, const uint32_t* zones, uint32_t depth,
                            powercap_zone_file type);

/* Return is like snprintf_base_path */
int snprintf_constraint_file_path(char* path, size_t size, const char* control_type, const uint32_t* zones,
                                  uint32_t depth, uint32_t constraint, powercap_constraint_file type);

/* Return fd on success, negative error code if path is too large, -1 on open failure */
int open_control_type_file(char* path, size_t size, const char* control_type, powercap_control_type_file type,
                           int flags);

/* Return fd on success, negative error code if path is too large, -1 on open failure */
int open_zone_file(char* path, size_t size, const char* control_type, const uint32_t* zones, uint32_t depth,
                   powercap_zone_file type, int flags);

/* Return fd on success, negative error code if path is too large, -1 on open failure */
int open_constraint_file(char* path, size_t size, const char* control_type, const uint32_t* zones, uint32_t depth,
                         uint32_t constraint, powercap_constraint_file type, int flags);

/* Return fd on success or ENOENT, -1 if buf is too small or on open failure */
int powercap_control_type_file_open(char* buf, size_t bsize, const char* ct_name, powercap_control_type_file type,
                                    int flags);

/* Return fd on success or ENOENT, -1 if buf is too small or on open failure */
int powercap_zone_file_open(char* buf, size_t bsize, const char* ct_name, const uint32_t* zones, uint32_t depth,
                            powercap_zone_file type, int flags);

/* Return fd on success or ENOENT, -1 if buf is too small or on open failure */
int powercap_constraint_file_open(char* buf, size_t bsize, const char* ct_name, const uint32_t* zones, uint32_t depth,
                                  uint32_t constraint, powercap_constraint_file type, int flags);

/*
 * Open all files in a control type, if they exist.
 * Return 0 on success or ENOENT, -1 if buf is too small or on open failure.
 */
int powercap_control_type_open(powercap_control_type* pct, char* buf, size_t bsize, const char* ct_name, int ro);

/*
 * Open all files in a zone, if they exist.
 * Return 0 on success or ENOENT, -1 if buf is too small or on open failure.
 */
int powercap_zone_open(powercap_zone* pz, char* buf, size_t bsize, const char* ct_name, const uint32_t* zones,
                       uint32_t depth, int ro);

/*
 * Open all files in a constraint, if they exist.
 * Return 0 on success or ENOENT, -1 if buf is too small or on open failure.
 */
int powercap_constraint_open(powercap_constraint* pc, char* buf, size_t bsize, const char* ct_name,
                             const uint32_t* zones, uint32_t depth, uint32_t constraint, int ro);

/*
 * Close all files in a control type.
 * Return 0 on success, negative error code on failure.
 */
int powercap_control_type_close(powercap_control_type* pct);

/*
 * Close all files in a zone.
 * Return 0 on success, negative error code on failure.
 */
int powercap_zone_close(powercap_zone* pz);

/*
 * Close all files in a constraint.
 * Return 0 on success, negative error code on failure.
 */
int powercap_constraint_close(powercap_constraint* pc);

#pragma GCC visibility pop

#ifdef __cplusplus
}
#endif

#endif