596f414d3ad7ee1d70c97d0c59a13352f393a0fc
[platform/core/system/pass.git] / include / util / resource.h
1 /*
2  * PASS
3  *
4  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #ifndef __RESOURCE_H__
21 #define __RESOURCE_H__
22
23 #include <sys/time.h>
24 #include <errno.h>
25 #include <glib.h>
26 #include "common.h"
27
28 /* Resource flags */
29 /*
30  * This flags indicates that the resource has the only one resource count
31  * such as system/memory resources.
32  */
33 #define RESOURCE_FLAG_COUNT_ONLY_ONE                    BIT(0)
34
35 /*
36  * This flag incidates that resource is either process or process-group
37  * and is not physical h/w resource.
38  */
39 #define RESOURCE_FLAG_PROCESS                           BIT(1)
40
41 struct resource;
42 struct resource_attribute;
43
44 struct array_value {
45         int type;
46         int length;
47         void *data;
48 };
49
50 struct resource_attribute_value {
51         int type;
52         void *data;
53 };
54
55 struct resource_attribute_ops {
56         int (*set)(struct resource *res,
57                    const struct resource_attribute *attr,
58                    const void *data, int count);
59         int (*get)(struct resource *res,
60                    const struct resource_attribute *attr,
61                    void *data);
62         /*
63          * If .is_supported ops is not implemented, use .get ops in order to
64          * check whether the resource attribute is supported or not.
65          */
66         bool (*is_supported)(struct resource *res,
67                    const struct resource_attribute *attr);
68 };
69
70 struct resource_attribute {
71         const char name[BUFF_MAX];
72         const u_int64_t id;
73         const int type;
74         const struct resource_attribute_ops ops;
75 };
76
77 struct resource_control;
78
79 struct resource_control_ops {
80         const int (*set)(struct resource *res,
81                          const struct resource_control *ctrl,
82                          const void *data);
83         const int (*get)(struct resource *res,
84                          const struct resource_control *ctrl,
85                          void **data);
86 };
87
88 struct resource_control {
89         const char name[BUFF_MAX];
90         const u_int64_t id;
91         const struct resource_control_ops ops;
92 };
93
94 struct resource_driver_ops {
95         int (*init)(struct resource *res);
96         void (*exit)(struct resource *res);
97         /*
98          * If prepare_update is specified, it will be called
99          * at every update_resource_attrs().
100          */
101         int (*prepare_update)(struct resource *res);
102 };
103
104 struct resource_driver {
105         const char *name;
106         const int type;
107         const u_int64_t flag;
108         const int num_attrs;
109         const struct resource_attribute *attrs;
110         const int num_ctrls;
111         const struct resource_control *ctrls;
112         const struct resource_driver_ops ops;
113 };
114
115 struct resource_device {
116         char *name;
117         int type;
118
119         /*
120          * Never initialize it by user of add_resource_device().
121          * It will be initialized by add_resource_device function automatically.
122          */
123         int index;
124 };
125
126 #define RESOURCE_DRIVER_REGISTER(resource_driver)       \
127 static void __CONSTRUCTOR__ module_init(void)   \
128 {       \
129         add_resource_driver(resource_driver);   \
130 }       \
131 static void __DESTRUCTOR__ module_exit(void)    \
132 {       \
133         remove_resource_driver(resource_driver);        \
134 }
135
136 /* Add/remove resource driver and device */
137 void add_resource_driver(const struct resource_driver *resource_driver);
138 void remove_resource_driver(const struct resource_driver *resource_driver);
139
140 int get_resource_device_count_all(void);
141 int get_resource_device_count(int resource_type);
142 const struct resource_device *find_resource_device(int resource_type, int resource_index);
143 int add_resource_device(struct resource_device *resource_device);
144 void remove_resource_device(struct resource_device *resource_device);
145
146 /* Create/delete resource instance */
147 struct resource *create_resource(int resource_type);
148 void delete_resource(struct resource *resource);
149
150 /* Handle resource control */
151 int set_resource_control(struct resource *resource, u_int64_t ctrl_id, const void *data);
152 const char *get_resource_control_name(struct resource *resource, u_int64_t ctrl_id);
153
154 /* Handle resource attribute */
155 int update_resource_attrs(struct resource *resource);
156 const struct resource_attribute *get_resource_attr(struct resource *resource, u_int64_t attr_id);
157 struct resource_attribute_value *
158 get_resource_attr_value(struct resource *resource, u_int64_t attr_id);
159 int is_resource_attr_supported(struct resource *resource, u_int64_t attr_id, bool *supported);
160
161 static inline bool
162 resource_attr_supported_always(struct resource *resource,
163                                const struct resource_attribute *attr)
164 {
165         return true;
166 }
167
168 int get_resource_attrs_json(struct resource *resource, char **json_string);
169 int get_resource_attr_json(struct resource *resource, u_int64_t attr_id, char **json_string);
170 int get_resource_list_json(char **json_string);
171
172 int get_resource_attr_int(struct resource *resource, u_int64_t attr_id, int32_t *data);
173 int get_resource_attr_int64(struct resource *resource, u_int64_t attr_id, int64_t *data);
174 int get_resource_attr_uint(struct resource *resource, u_int64_t attr_id, u_int32_t *data);
175 int get_resource_attr_uint64(struct resource *resource, u_int64_t attr_id, u_int64_t *data);
176 int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, double *data);
177 int get_resource_attr_string(struct resource *resource, u_int64_t attr_id, char *data);
178 int get_resource_attr_array(struct resource *resource, u_int64_t attr_id,
179                             struct array_value **data);
180 int get_resource_attr_ptr(struct resource *resource, u_int64_t attr_id, void **data);
181
182 int set_resource_attr_interest(struct resource *resource, u_int64_t interest_mask);
183 int unset_resource_attr_interest(struct resource *resource, u_int64_t interest_mask);
184 bool is_resource_attr_interested(struct resource *resource, u_int64_t interest_mask);
185 const char *get_resource_attr_name(struct resource *resource, u_int64_t attr_id);
186
187 const char *get_resource_name(struct resource *resource);
188 void *get_resource_privdata(struct resource *resource);
189 int get_resource_id(struct resource *resource);
190 int get_resource_type(struct resource *resource);
191 int get_resource_ts(struct resource *resource, int64_t *ts_start, int64_t *ts_end);
192 void init_resource_id(void);
193
194 int set_resource_privdata(struct resource *resource, void *priv);
195
196 inline __attribute__((always_inline)) int64_t get_time_now(void)
197 {
198         struct timeval tv;
199
200         gettimeofday(&tv, NULL);
201         return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;
202 }
203 #endif