EM-ODP 4.4.0
Event Machine on ODP
Loading...
Searching...
No Matches
event_machine_helper.c
1/*
2 * Copyright (c) 2012, Nokia Siemens Networks
3 * Copyright (c) 2015-2023, Nokia Solutions and Networks
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * * Neither the name of the copyright holder nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32#ifndef _GNU_SOURCE
33#define _GNU_SOURCE
34#endif
35
36#ifdef HAVE_CONFIG_H
37#include "config.h"
38#endif
39
40#include <errno.h>
41#include <libgen.h> /* basename() */
42#include <stdbool.h>
43#include <stdint.h>
44#include <stdio.h>
45#include <string.h>
46#include <unistd.h> /* ssize_t */
47
48#include <odp_api.h>
49
50#include <event_machine.h>
54
55#include "em_core.h"
56#include "em_error.h"
57#include "em_event.h"
58#include "em_event_inline.h"
59#include "em_event_types.h"
60#include "em_info.h"
61#include "em_mem.h"
62#include "em_pool.h"
63#include "em_pool_types.h"
64
65int em_error_format_string(char err_msg[/*out*/], size_t errmsg_sz, em_eo_t eo,
66 em_status_t error, em_escope_t escope, va_list args)
67{
68 if (!EM_ESCOPE(escope) || !err_msg || (ssize_t)errmsg_sz <= 0)
69 return 0;
70
71 char eo_str[sizeof("EO:xxxxxx-abdc ") + EM_EO_NAME_LEN];
72
73 eo_str[0] = '\0';
74
75 if (eo != EM_EO_UNDEF) {
76 char eo_name[EM_EO_NAME_LEN];
77
78 em_eo_name(eo, eo_name, sizeof(eo_name));
79 snprintf(eo_str, sizeof(eo_str),
80 "EO:%" PRI_EO "-\"%s\" ", eo, eo_name);
81 /* Both eo_name[] and eo_str[] always '\0'-terminated */
82 }
83
84 size_t len = format_errmsg(error, escope, eo_str, args, false,
85 err_msg /*out*/, errmsg_sz);
86
87 return (int)len + 1;
88}
89
91{
92 print_version_info();
93}
94
95void em_info_print(void)
96{
97 print_em_info();
98}
99
101{
102 const em_locm_t *const locm = &em_locm;
103 int phys_core;
104
105 if (unlikely((const unsigned int)em_core_id >= EM_MAX_CORES))
106 return -1;
107
108 if (locm->core_id == em_core_id)
109 return odp_cpu_id();
110
111 core_map_t *const core_map = &em_shm->core_map;
112 char path[32]; /* path to /proc/[tid]/stat */
113
114 odp_rwlock_read_lock(&core_map->rwlock);
115 memcpy(path, core_map->rwlocked.proc_tid_stat_paths[em_core_id], sizeof(path));
116 odp_rwlock_read_unlock(&core_map->rwlock);
117
118 if (unlikely(path[0] == '\0'))
119 return -1;
120
121 FILE *file = fopen(path, "r");
122
123 if (!file) {
124 EM_LOG(EM_LOG_ERR, "fopen(%s) failed: %s (%d)\n",
125 path, strerror(errno), errno);
126 return -1;
127 }
128
129 phys_core = read_proc_cpuid(file);
130 fclose(file);
131
132 return phys_core;
133}
134
135void em_core_mask_physical(em_core_mask_t *mask_phys /*out*/,
136 const em_core_mask_t *mask_emcore)
137{
138 core_map_t *const core_map = &em_shm->core_map;
139 const int mask_cnt = em_core_mask_count(mask_emcore);
140 int core_em = em_core_mask_first(mask_emcore);
141
142 em_core_mask_zero(mask_phys);
143
144 if (unlikely(mask_cnt <= 0 || mask_cnt > EM_MAX_CORES || core_em < 0))
145 return;
146
147 char paths[mask_cnt][32]; /* paths to /proc/[tid]/stat */
148
149 odp_rwlock_read_lock(&core_map->rwlock);
150
151 int f_cnt = 0;
152
153 while (core_em >= 0 && f_cnt < mask_cnt) {
154 memcpy(paths[f_cnt], core_map->rwlocked.proc_tid_stat_paths[core_em],
155 sizeof(paths[f_cnt]));
156
157 if (unlikely(paths[f_cnt][0] == '\0')) {
158 odp_rwlock_read_unlock(&core_map->rwlock);
159 return;
160 }
161 core_em = em_core_mask_next(mask_emcore, core_em);
162 f_cnt++;
163 }
164
165 odp_rwlock_read_unlock(&core_map->rwlock);
166
167 for (int i = 0; i < f_cnt; i++) {
168 FILE *file = fopen(paths[i], "r");
169 int phys_core;
170
171 if (!file) {
172 EM_LOG(EM_LOG_ERR, "fopen(%s) failed: %s (%d)\n",
173 paths[i], strerror(errno), errno);
174 return;
175 }
176 phys_core = read_proc_cpuid(file);
177 fclose(file);
178
179 if (phys_core >= 0)
180 em_core_mask_set(phys_core, mask_phys);
181 }
182}
183
185{
187 return 0;
188
189 if (unlikely(tsp >= EM_DEBUG_TSP_LAST || tsp < 0))
190 return 0;
191 else
192 return em_locm.debug_ts[tsp];
193}
ENV_LOCAL em_locm_t em_locm
em_shm_t * em_shm
#define EM_DEBUG_TIMESTAMP_ENABLE
#define EM_EO_NAME_LEN
#define EM_MAX_CORES
int em_core_mask_count(const em_core_mask_t *mask)
int em_core_mask_first(const em_core_mask_t *mask)
void em_core_mask_set(int core, em_core_mask_t *mask)
int em_core_mask_next(const em_core_mask_t *mask, int idx)
void em_core_mask_zero(em_core_mask_t *mask)
uint64_t em_debug_timestamp(em_debug_tsp_t tsp)
void em_core_mask_physical(em_core_mask_t *phys, const em_core_mask_t *logic)
void em_version_print(void)
Print EM related version information.
int em_core_id_physical(int core)
void em_info_print(void)
Print miscellaneous EM information.
int em_error_format_string(char *str, size_t size, em_eo_t eo, em_status_t error, em_escope_t escope, va_list args)
#define PRI_EO
#define EM_EO_UNDEF
int em_core_id(void)
size_t em_eo_name(em_eo_t eo, char *name, size_t maxlen)
uint32_t em_escope_t
#define EM_ESCOPE(escope)
uint32_t em_status_t
odp_rwlock_t rwlock
char proc_tid_stat_paths[EM_MAX_CORES][32]
uint64_t debug_ts[EM_DEBUG_TSP_LAST]
Definition em_mem.h:289
int core_id
Definition em_mem.h:239