2 * This file is part of stability-monitor
4 * Copyright © 2019 Samsung Electronics
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.
30 unsigned long long time_now(void)
34 if (clock_gettime(CLOCK_BOOTTIME, &ts) == -1) {
35 _W("Couldn't get current clock time: %m");
38 return ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC; // us
41 char *get_basename(char *path)
43 char *base = strrchr(path, '/');
44 return base ? base+1 : path;
47 int pid_to_basename(int pid, char *basename, size_t basename_buflen)
49 char path[PATH_MAX + 1];
50 char link_name[NAME_MAX];
55 /* Assembly /proc/pid/exe */
56 ret = snprintf(link_name, sizeof(link_name), PROC_EXE_PATH, pid);
61 /* Readlink /proc/pid/exe (leaving space for the null terminator
62 * which readlink never appends on its own) */
63 ret = readlink(link_name, path, sizeof(path) - 1);
65 _DD("readlink() failed, pid: %d, ret: %d\n", pid, ret);
72 basename_ptr = get_basename(path);
73 basename_len = strlen(basename_ptr);
75 if (basename_len >= basename_buflen)
78 strncpy(basename, basename_ptr, basename_buflen);
79 basename[basename_len] = 0;
84 const char *smpl2str(struct sample_s *s)
86 static char str[MAX_SAMPLE_STR];
89 if (s->type == ST_DOUBLE)
90 r = snprintf(str, MAX_SAMPLE_STR, "%2.3lf", s->value.d);
91 else if (s->type == ST_INT)
92 r = snprintf(str, MAX_SAMPLE_STR, "%d", s->value.i);
93 else if (s->type == ST_ULL)
94 r = snprintf(str, MAX_SAMPLE_STR, "%llu", s->value.ull);
101 _W("Couldn't parse sample: %m");
109 int _json_object_object_merge(json_object *obj1, char *key, json_object *obj2_node)
111 struct json_object *obj1_node = NULL;
114 ret = json_object_object_get_ex(obj1, key, &obj1_node);
116 /* if there is no such object in obj1 - add it */
120 if (obj1_node == NULL) {
121 _E("Unable to get json object");
125 /* if there is, but obj1 or obj2 is not json_type_object - replace it */
126 if (!json_object_is_type(obj1_node, json_type_object) ||
127 !json_object_is_type(obj2_node, json_type_object)) {
129 json_object_object_del(obj1, key);
133 /* if obj1 and obj2 is json_object_type */
134 json_object_object_foreach(obj2_node, k, v) {
135 ret = _json_object_object_merge(obj1_node, k, v);
143 json_object_get(obj2_node);
144 json_object_object_add(obj1, key, obj2_node);