3 * Copyright (c) 2020 Samsung Electronics Co., Ltd
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is furnished to do
10 * so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in all
13 * copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #define INITIAL_CAPACITY 128
31 struct metrics_info *inner;
32 int count; // entries in the vector
33 int total; // total logs (entries in vector * entry count)
37 struct metrics *metrics_create()
39 return calloc(1, sizeof(struct metrics));
42 void metrics_destroy(struct metrics *m)
47 for (int i = 0; i < m->count; ++i)
48 free(m->inner[i].tag);
54 bool metrics_add_log(struct metrics *m, const dlogutil_entry_s *e)
62 if (dlogutil_entry_get_tag(e, &tag))
64 if (dlogutil_entry_get_pid(e, &pid))
66 if (dlogutil_entry_get_priority(e, &prio))
69 for (int i = 0; i < m->count; ++i) {
70 if (pid != m->inner[i].pid || strcmp(tag, m->inner[i].tag) != 0)
73 assert(prio < DLOG_PRIO_MAX);
74 m->inner[i].count[prio] += 1;
79 if (m->capacity == 0) {
80 m->inner = calloc(INITIAL_CAPACITY, sizeof(*m->inner));
83 m->capacity = INITIAL_CAPACITY;
84 } else if (m->count == m->capacity) {
85 typeof(m->inner) tmp = realloc(m->inner, 2 * m->capacity * sizeof *tmp);
92 char *const tag_copy = strdup(tag);
95 m->inner[m->count].tag = tag_copy;
96 m->inner[m->count].pid = pid;
97 for (int i = 0; i < (int) DLOG_PRIO_MAX; ++i)
98 m->inner[m->count].count[i] = 0;
99 m->inner[m->count].count[prio] = 1;
105 int metrics_get_total(const struct metrics *m)
112 struct metrics_info *metrics_get_info(const struct metrics *m, int *count)
122 struct metrics_info *ret = calloc(m->count, sizeof(*ret));
129 memcpy(ret, m->inner, m->count * sizeof(*ret));
133 void metrics_clear(struct metrics *m)
137 for (int i = 0; i < m->count; ++i)
138 free(m->inner[i].tag);
144 int metrics_sort_by_tag_first(const void *vlhs, const void *vrhs)
146 const struct metrics_info *lhs = vlhs, *rhs = vrhs;
147 int str_val = strcmp(lhs->tag, rhs->tag);
152 if (lhs->pid == rhs->pid)
155 return lhs->pid < rhs->pid ? -1 : 1;
158 int metrics_sort_by_pid_first(const void *vlhs, const void *vrhs)
160 const struct metrics_info *lhs = vlhs, *rhs = vrhs;
162 if (lhs->pid < rhs->pid)
164 else if (lhs->pid > rhs->pid)
167 return strcmp(lhs->tag, rhs->tag);