4 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 #ifndef __RESOURCE_H__
21 #define __RESOURCE_H__
30 * This flags indicates that the resource has the only one resource count
31 * such as system/memory resources.
33 #define RESOURCE_FLAG_COUNT_ONLY_ONE BIT(0)
36 * This flag incidates that resource is either process or process-group
37 * and is not physical h/w resource.
39 #define RESOURCE_FLAG_PROCESS BIT(1)
42 struct resource_attribute;
50 struct resource_attribute_value {
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,
63 * If .is_supported ops is not implemented, use .get ops in order to
64 * check whether the resource attribute is supported or not.
66 bool (*is_supported)(struct resource *res,
67 const struct resource_attribute *attr);
70 struct resource_attribute {
71 const char name[BUFF_MAX];
74 const struct resource_attribute_ops ops;
77 struct resource_control;
79 struct resource_control_ops {
80 const int (*set)(struct resource *res,
81 const struct resource_control *ctrl,
83 const int (*get)(struct resource *res,
84 const struct resource_control *ctrl,
88 struct resource_control {
89 const char name[BUFF_MAX];
91 const struct resource_control_ops ops;
94 struct resource_driver_ops {
97 int (*create)(struct resource *res);
98 void (*delete)(struct resource *res);
100 * If prepare_update is specified, it will be called
101 * at every update_resource_attrs().
103 int (*prepare_update)(struct resource *res);
106 struct resource_driver {
109 const u_int64_t flag;
111 const struct resource_attribute *attrs;
113 const struct resource_control *ctrls;
114 const struct resource_driver_ops ops;
117 struct resource_device {
122 * Never initialize it by user of add_resource_device().
123 * It will be initialized by add_resource_device function automatically.
128 #define RESOURCE_DRIVER_REGISTER(resource_driver) \
129 static void __CONSTRUCTOR__ module_init(void) \
131 add_resource_driver(resource_driver); \
133 static void __DESTRUCTOR__ module_exit(void) \
135 remove_resource_driver(resource_driver); \
138 /* Add/remove resource driver and device */
139 void add_resource_driver(const struct resource_driver *resource_driver);
140 void remove_resource_driver(const struct resource_driver *resource_driver);
142 int get_resource_device_count_all(void);
143 int get_resource_device_count(int resource_type);
144 const struct resource_device *find_resource_device(int resource_type, int resource_index);
145 int add_resource_device(struct resource_device *resource_device);
146 void remove_resource_device(struct resource_device *resource_device);
148 /* Create/delete resource instance */
149 struct resource *create_resource(int resource_type);
150 void delete_resource(struct resource *resource);
152 /* Handle resource control */
153 int set_resource_control(struct resource *resource, u_int64_t ctrl_id, const void *data);
154 const char *get_resource_control_name(struct resource *resource, u_int64_t ctrl_id);
156 /* Handle resource attribute */
157 int update_resource_attrs(struct resource *resource);
158 const struct resource_attribute *get_resource_attr(struct resource *resource, u_int64_t attr_id);
159 struct resource_attribute_value *
160 get_resource_attr_value(struct resource *resource, u_int64_t attr_id);
161 int is_resource_attr_supported(struct resource *resource, u_int64_t attr_id, bool *supported);
164 resource_attr_supported_always(struct resource *resource,
165 const struct resource_attribute *attr)
170 int get_resource_attrs_json(struct resource *resource, char **json_string);
171 int get_resource_attr_json(struct resource *resource, u_int64_t attr_id, char **json_string);
172 int get_resource_list_json(char **json_string);
174 int get_resource_attr_int(struct resource *resource, u_int64_t attr_id, int32_t *data);
175 int get_resource_attr_int64(struct resource *resource, u_int64_t attr_id, int64_t *data);
176 int get_resource_attr_uint(struct resource *resource, u_int64_t attr_id, u_int32_t *data);
177 int get_resource_attr_uint64(struct resource *resource, u_int64_t attr_id, u_int64_t *data);
178 int get_resource_attr_double(struct resource *resource, u_int64_t attr_id, double *data);
179 int get_resource_attr_string(struct resource *resource, u_int64_t attr_id, char *data);
180 int get_resource_attr_array(struct resource *resource, u_int64_t attr_id,
181 struct array_value **data);
182 int get_resource_attr_ptr(struct resource *resource, u_int64_t attr_id, void **data);
184 int set_resource_attr_interest(struct resource *resource, u_int64_t interest_mask);
185 int unset_resource_attr_interest(struct resource *resource, u_int64_t interest_mask);
186 bool is_resource_attr_interested(struct resource *resource, u_int64_t interest_mask);
187 const char *get_resource_attr_name(struct resource *resource, u_int64_t attr_id);
189 const char *get_resource_name(struct resource *resource);
190 void *get_resource_privdata(struct resource *resource);
191 int get_resource_id(struct resource *resource);
192 int get_resource_type(struct resource *resource);
193 int get_resource_ts(struct resource *resource, int64_t *ts_start, int64_t *ts_end);
194 void init_resource_id(void);
196 int set_resource_privdata(struct resource *resource, void *priv);
198 inline __attribute__((always_inline)) int64_t get_time_now(void)
202 gettimeofday(&tv, NULL);
203 return (int64_t)tv.tv_sec * 1000000 + (int64_t)tv.tv_usec;