From 79cca793dbc340a619366c0c48669e21be9cb8c5 Mon Sep 17 00:00:00 2001 From: Guillaume Fieni Date: Wed, 7 Feb 2024 21:56:20 +0200 Subject: [PATCH 1/2] refactor: Adapt codebase for C++ compilation --- src/config.c | 4 +-- src/config_cli.c | 4 +-- src/config_json.c | 2 +- src/events.c | 12 ++++---- src/hwinfo.c | 18 +++++------ src/payload.c | 8 ++--- src/perf.c | 68 ++++++++++++++++++++++------------------- src/pmu.c | 12 ++++---- src/report.c | 8 ++--- src/sensor.c | 14 ++++----- src/storage_csv.c | 42 ++++++++++++------------- src/storage_mongodb.c | 30 +++++++++--------- src/storage_null.c | 2 +- src/storage_socket.c | 34 ++++++++++----------- src/target.c | 21 +++++-------- src/target.h | 5 --- src/target_docker.c | 2 +- src/target_kubernetes.c | 2 +- src/util.c | 4 +-- 19 files changed, 143 insertions(+), 149 deletions(-) diff --git a/src/config.c b/src/config.c index dc14eae..b759be5 100644 --- a/src/config.c +++ b/src/config.c @@ -46,7 +46,7 @@ struct config * config_create(void) { - struct config *config = malloc(sizeof(struct config)); + struct config *config = (struct config *) malloc(sizeof(struct config)); if (!config) return NULL; @@ -104,7 +104,7 @@ is_events_group_empty(zhashx_t *events_groups) { struct events_group *events_group = NULL; - for (events_group = zhashx_first(events_groups); events_group; events_group = zhashx_next(events_groups)) { + for (events_group = (struct events_group *) zhashx_first(events_groups); events_group; events_group = (struct events_group *) zhashx_next(events_groups)) { if (zlistx_size(events_group->events) == 0) { zsys_error("config: Events group '%s' is empty", events_group->name); return -1; diff --git a/src/config_cli.c b/src/config_cli.c index 467078b..fc467bf 100644 --- a/src/config_cli.c +++ b/src/config_cli.c @@ -329,14 +329,14 @@ config_setup_from_cli(int argc, char **argv, struct config *config) if (setup_global_events_group(config, optarg)) { return -1; } - current_events_group = zhashx_lookup(config->events.system, optarg); + current_events_group = (struct events_group *) zhashx_lookup(config->events.system, optarg); break; case 'c': if (setup_cgroups_events_group(config, optarg)) { return -1; } - current_events_group = zhashx_lookup(config->events.containers, optarg); + current_events_group = (struct events_group *) zhashx_lookup(config->events.containers, optarg); break; case 'o': diff --git a/src/config_json.c b/src/config_json.c index 747ed00..332c24e 100644 --- a/src/config_json.c +++ b/src/config_json.c @@ -469,7 +469,7 @@ static int parse_json_configuration_file_from_fd(int fd, json_object **obj) { int ret = -1; - char buffer[JSON_FILE_BUFFER_SIZE] = {0}; + char buffer[JSON_FILE_BUFFER_SIZE] = {}; json_tokener *tok = NULL; enum json_tokener_error jerr; size_t line, column; diff --git a/src/events.c b/src/events.c index e178e73..0df59d4 100644 --- a/src/events.c +++ b/src/events.c @@ -99,7 +99,7 @@ get_msr_pmu_event_encoding(const char *event_name, struct perf_event_attr *attr) static int setup_perf_event_attr(const char *event_name, struct perf_event_attr *attr) { - pfm_perf_encode_arg_t arg = {0}; + pfm_perf_encode_arg_t arg = {}; attr->size = sizeof(struct perf_event_attr); attr->disabled = 1; @@ -120,11 +120,11 @@ setup_perf_event_attr(const char *event_name, struct perf_event_attr *attr) struct event_config * event_config_create(const char *event_name) { - struct perf_event_attr attr = {0}; + struct perf_event_attr attr = {}; struct event_config *config = NULL; if (!setup_perf_event_attr(event_name, &attr)) { - config = malloc(sizeof(struct event_config)); + config = (struct event_config *) malloc(sizeof(struct event_config)); if (config) { snprintf(config->name, NAME_MAX, "%s", event_name); config->attr = attr; @@ -140,7 +140,7 @@ event_config_dup(struct event_config *config) struct event_config *copy = NULL; if (config) { - copy = malloc(sizeof(struct event_config)); + copy = (struct event_config *) malloc(sizeof(struct event_config)); if (copy) { snprintf(copy->name, NAME_MAX, "%s", config->name); copy->attr = config->attr; @@ -162,7 +162,7 @@ event_config_destroy(struct event_config **config) struct events_group * events_group_create(const char *name) { - struct events_group *group = malloc(sizeof(struct events_group)); + struct events_group *group = (struct events_group *) malloc(sizeof(struct events_group)); if (group) { snprintf(group->name, NAME_MAX, "%s", name); @@ -182,7 +182,7 @@ events_group_dup(struct events_group *group) struct events_group *copy = NULL; if (group) { - copy = malloc(sizeof(struct events_group)); + copy = (struct events_group *) malloc(sizeof(struct events_group)); if (copy) { snprintf(copy->name, NAME_MAX, "%s", group->name); copy->type = group->type; diff --git a/src/hwinfo.c b/src/hwinfo.c index b0d3201..b2b4611 100644 --- a/src/hwinfo.c +++ b/src/hwinfo.c @@ -52,7 +52,7 @@ static struct hwinfo_pkg * hwinfo_pkg_create(void) { - struct hwinfo_pkg *pkg = malloc(sizeof(struct hwinfo_pkg)); + struct hwinfo_pkg *pkg = (struct hwinfo_pkg *) malloc(sizeof(struct hwinfo_pkg)); if (!pkg) return NULL; @@ -67,7 +67,7 @@ hwinfo_pkg_create(void) static struct hwinfo_pkg * hwinfo_pkg_dup(struct hwinfo_pkg *pkg) { - struct hwinfo_pkg *pkgcpy = malloc(sizeof(struct hwinfo_pkg)); + struct hwinfo_pkg *pkgcpy = (struct hwinfo_pkg *) malloc(sizeof(struct hwinfo_pkg)); if (!pkgcpy) return NULL; @@ -92,7 +92,7 @@ static int get_cpu_online_status(const char *cpu_dir) { int status = 1; - char path[PATH_MAX] = {0}; + char path[PATH_MAX] = {}; FILE *f = NULL; char buffer[2]; /* boolean expected */ @@ -118,7 +118,7 @@ static char * get_package_id(const char *cpu_dir) { FILE *f = NULL; - char path[PATH_MAX] = {0}; + char path[PATH_MAX] = {}; char buffer[24]; /* log10(ULLONG_MAX) */ char *id = NULL; @@ -138,7 +138,7 @@ get_package_id(const char *cpu_dir) static char * parse_cpu_id_from_name(const char *str) { - regex_t re = {0}; + regex_t re = {}; regmatch_t matches[CPU_ID_REGEX_EXPECTED_MATCHES]; char *id = NULL; @@ -191,7 +191,7 @@ do_packages_detection(struct hwinfo *hwinfo) } /* get cpu pkg or create it if never encountered */ - pkg = zhashx_lookup(hwinfo->pkgs, pkg_id); + pkg = (struct hwinfo_pkg *) zhashx_lookup(hwinfo->pkgs, pkg_id); if (!pkg) { pkg = hwinfo_pkg_create(); if (!pkg) { @@ -201,7 +201,7 @@ do_packages_detection(struct hwinfo *hwinfo) zhashx_insert(hwinfo->pkgs, pkg_id, pkg); hwinfo_pkg_destroy(&pkg); - pkg = zhashx_lookup(hwinfo->pkgs, pkg_id); /* get the copy the pkg done by zhashx_insert */ + pkg = (struct hwinfo_pkg *) zhashx_lookup(hwinfo->pkgs, pkg_id); /* get the copy the pkg done by zhashx_insert */ } zlistx_add_end(pkg->cpus_id, cpu_id); @@ -235,7 +235,7 @@ hwinfo_detect(struct hwinfo *hwinfo) struct hwinfo * hwinfo_create(void) { - struct hwinfo *hw = malloc(sizeof(struct hwinfo)); + struct hwinfo *hw = (struct hwinfo *) malloc(sizeof(struct hwinfo)); if (!hw) return NULL; @@ -250,7 +250,7 @@ hwinfo_create(void) struct hwinfo * hwinfo_dup(struct hwinfo *hwinfo) { - struct hwinfo *hwinfocpy = malloc(sizeof(struct hwinfo)); + struct hwinfo *hwinfocpy = (struct hwinfo *) malloc(sizeof(struct hwinfo)); if (!hwinfocpy) return NULL; diff --git a/src/payload.c b/src/payload.c index cc8072d..ad9c386 100644 --- a/src/payload.c +++ b/src/payload.c @@ -38,7 +38,7 @@ struct payload_cpu_data * payload_cpu_data_create(void) { - struct payload_cpu_data *data = malloc(sizeof(struct payload_cpu_data)); + struct payload_cpu_data *data = (struct payload_cpu_data *) malloc(sizeof(struct payload_cpu_data)); if (!data) return NULL; @@ -64,7 +64,7 @@ payload_cpu_data_destroy(struct payload_cpu_data **data_ptr) struct payload_pkg_data * payload_pkg_data_create(void) { - struct payload_pkg_data *data = malloc(sizeof(struct payload_pkg_data)); + struct payload_pkg_data *data = (struct payload_pkg_data *) malloc(sizeof(struct payload_pkg_data)); if (!data) return NULL; @@ -89,7 +89,7 @@ payload_pkg_data_destroy(struct payload_pkg_data **data_ptr) struct payload_group_data * payload_group_data_create(void) { - struct payload_group_data *data = malloc(sizeof(struct payload_group_data)); + struct payload_group_data *data = (struct payload_group_data *) malloc(sizeof(struct payload_group_data)); if (!data) return NULL; @@ -114,7 +114,7 @@ payload_group_data_destroy(struct payload_group_data **data_ptr) struct payload * payload_create(uint64_t timestamp, const char *target_name) { - struct payload *payload = malloc(sizeof(struct payload)); + struct payload *payload = (struct payload *) malloc(sizeof(struct payload)); if (!payload) return NULL; diff --git a/src/perf.c b/src/perf.c index 09e4520..9d8f934 100644 --- a/src/perf.c +++ b/src/perf.c @@ -49,7 +49,7 @@ struct perf_config * perf_config_create(struct hwinfo *hwinfo, zhashx_t *events_groups, struct target *target) { - struct perf_config *config = malloc(sizeof(struct perf_config)); + struct perf_config *config = (struct perf_config *) malloc(sizeof(struct perf_config)); if (!config) return NULL; @@ -87,7 +87,7 @@ perf_event_fd_destroy(int **fd_ptr) static struct perf_group_cpu_context * perf_group_cpu_context_create(void) { - struct perf_group_cpu_context *ctx = malloc(sizeof(struct perf_group_cpu_context)); + struct perf_group_cpu_context *ctx = (struct perf_group_cpu_context *) malloc(sizeof(struct perf_group_cpu_context)); if (!ctx) return NULL; @@ -113,7 +113,7 @@ perf_group_cpu_context_destroy(struct perf_group_cpu_context **ctx) static struct perf_group_pkg_context * perf_group_pkg_context_create(void) { - struct perf_group_pkg_context *ctx = malloc(sizeof(struct perf_group_pkg_context)); + struct perf_group_pkg_context *ctx = (struct perf_group_pkg_context *) malloc(sizeof(struct perf_group_pkg_context)); if (!ctx) return NULL; @@ -138,7 +138,7 @@ perf_group_pkg_context_destroy(struct perf_group_pkg_context **ctx) static struct perf_group_context * perf_group_context_create(struct events_group *group) { - struct perf_group_context *ctx = malloc(sizeof(struct perf_group_context)); + struct perf_group_context *ctx = (struct perf_group_context *) malloc(sizeof(struct perf_group_context)); if (!ctx) return NULL; @@ -164,7 +164,7 @@ perf_group_context_destroy(struct perf_group_context **ctx) static struct perf_context * perf_context_create(struct perf_config *config, zsock_t *pipe, const char *target_name) { - struct perf_context *ctx = malloc(sizeof(struct perf_context)); + struct perf_context *ctx = (struct perf_context *) malloc(sizeof(struct perf_context)); if (!ctx) return NULL; @@ -217,7 +217,7 @@ perf_events_group_setup_cpu(struct perf_context *ctx, struct perf_group_cpu_cont return -1; } - for (event = zlistx_first(group->events); event; event = zlistx_next(group->events)) { + for (event = (struct event_config *) zlistx_first(group->events); event; event = (struct event_config *) zlistx_next(group->events)) { errno = 0; perf_fd = perf_event_open(&event->attr, ctx->cgroup_fd, (int) cpu, group_fd, perf_flags); if (perf_fd < 1) { @@ -258,8 +258,8 @@ perf_events_groups_initialize(struct perf_context *ctx) } } - for (events_group = zhashx_first(ctx->config->events_groups); events_group; events_group = zhashx_next(ctx->config->events_groups)) { - events_group_name = zhashx_cursor(ctx->config->events_groups); + for (events_group = (struct events_group *) zhashx_first(ctx->config->events_groups); events_group; events_group = (struct events_group *) zhashx_next(ctx->config->events_groups)) { + events_group_name = (const char *) zhashx_cursor(ctx->config->events_groups); /* create group context */ group_ctx = perf_group_context_create(events_group); @@ -268,8 +268,8 @@ perf_events_groups_initialize(struct perf_context *ctx) goto error; } - for (pkg = zhashx_first(ctx->config->hwinfo->pkgs); pkg; pkg = zhashx_next(ctx->config->hwinfo->pkgs)) { - pkg_id = zhashx_cursor(ctx->config->hwinfo->pkgs); + for (pkg = (struct hwinfo_pkg *) zhashx_first(ctx->config->hwinfo->pkgs); pkg; pkg = (struct hwinfo_pkg *) zhashx_next(ctx->config->hwinfo->pkgs)) { + pkg_id = (const char *) zhashx_cursor(ctx->config->hwinfo->pkgs); /* create package context */ pkg_ctx = perf_group_pkg_context_create(); @@ -278,7 +278,7 @@ perf_events_groups_initialize(struct perf_context *ctx) goto error; } - for (cpu_id = zlistx_first(pkg->cpus_id); cpu_id; cpu_id = zlistx_next(pkg->cpus_id)) { + for (cpu_id = (const char *) zlistx_first(pkg->cpus_id); cpu_id; cpu_id = (const char *) zlistx_next(pkg->cpus_id)) { /* create cpu context */ cpu_ctx = perf_group_cpu_context_create(); if (!cpu_ctx) { @@ -328,15 +328,15 @@ perf_events_groups_enable(struct perf_context *ctx) const char *cpu_id = NULL; const int *group_leader_fd = NULL; - for (group_ctx = zhashx_first(ctx->groups_ctx); group_ctx; group_ctx = zhashx_next(ctx->groups_ctx)) { - group_name = zhashx_cursor(ctx->groups_ctx); + for (group_ctx = (struct perf_group_context *) zhashx_first(ctx->groups_ctx); group_ctx; group_ctx = (struct perf_group_context *) zhashx_next(ctx->groups_ctx)) { + group_name = (const char *) zhashx_cursor(ctx->groups_ctx); - for (pkg_ctx = zhashx_first(group_ctx->pkgs_ctx); pkg_ctx; pkg_ctx = zhashx_next(group_ctx->pkgs_ctx)) { - pkg_id = zhashx_cursor(group_ctx->pkgs_ctx); + for (pkg_ctx = (struct perf_group_pkg_context *) zhashx_first(group_ctx->pkgs_ctx); pkg_ctx; pkg_ctx = (struct perf_group_pkg_context *) zhashx_next(group_ctx->pkgs_ctx)) { + pkg_id = (const char *) zhashx_cursor(group_ctx->pkgs_ctx); - for (cpu_ctx = zhashx_first(pkg_ctx->cpus_ctx); cpu_ctx; cpu_ctx = zhashx_next(pkg_ctx->cpus_ctx)) { - cpu_id = zhashx_cursor(pkg_ctx->cpus_ctx); - group_leader_fd = zlistx_first(cpu_ctx->perf_fds); + for (cpu_ctx = (struct perf_group_cpu_context *) zhashx_first(pkg_ctx->cpus_ctx); cpu_ctx; cpu_ctx = (struct perf_group_cpu_context *) zhashx_next(pkg_ctx->cpus_ctx)) { + cpu_id = (const char *) zhashx_cursor(pkg_ctx->cpus_ctx); + group_leader_fd = (int *) zlistx_first(cpu_ctx->perf_fds); if (!group_leader_fd) { zsys_error("perf<%s>: no group leader fd for group=%s pkg=%s cpu=%s", ctx->target_name, group_name, pkg_id, cpu_id); continue; @@ -359,7 +359,7 @@ perf_events_group_read_cpu(struct perf_group_cpu_context *cpu_ctx, struct perf_r { int *group_leader_fd = NULL; - group_leader_fd = zlistx_first(cpu_ctx->perf_fds); + group_leader_fd = (int *) zlistx_first(cpu_ctx->perf_fds); if (!group_leader_fd) return -1; @@ -388,11 +388,13 @@ handle_pipe(struct perf_context *ctx) zstr_free(&command); } +#if 0 static inline double compute_perf_multiplexing_ratio(struct perf_read_format *report) { return (!report->time_enabled) ? 1.0 : (double) report->time_running / (double) report->time_enabled; } +#endif static int populate_payload(struct perf_context *ctx, struct payload *payload) @@ -408,12 +410,12 @@ populate_payload(struct perf_context *ctx, struct payload *payload) struct payload_cpu_data *cpu_data = NULL; size_t perf_read_buffer_size; struct perf_read_format *perf_read_buffer = NULL; - double perf_multiplexing_ratio; + // double perf_multiplexing_ratio; const struct event_config *event = NULL; int event_i; - for (group_ctx = zhashx_first(ctx->groups_ctx); group_ctx; group_ctx = zhashx_next(ctx->groups_ctx)) { - group_name = zhashx_cursor(ctx->groups_ctx); + for (group_ctx = (struct perf_group_context *) zhashx_first(ctx->groups_ctx); group_ctx; group_ctx = (struct perf_group_context *) zhashx_next(ctx->groups_ctx)) { + group_name = (const char *) zhashx_cursor(ctx->groups_ctx); group_data = payload_group_data_create(); if (!group_data) { zsys_error("perf<%s>: failed to allocate group data for group=%s", ctx->target_name, group_name); @@ -422,22 +424,22 @@ populate_payload(struct perf_context *ctx, struct payload *payload) /* shared perf read buffer */ perf_read_buffer_size = offsetof(struct perf_read_format, values) + sizeof(struct perf_counter_value[zlistx_size(group_ctx->config->events)]); - perf_read_buffer = malloc(perf_read_buffer_size); + perf_read_buffer = (struct perf_read_format *) malloc(perf_read_buffer_size); if (!perf_read_buffer) { zsys_error("perf<%s>: failed to allocate perf read buffer for group=%s", ctx->target_name, group_name); goto error; } - for (pkg_ctx = zhashx_first(group_ctx->pkgs_ctx); pkg_ctx; pkg_ctx = zhashx_next(group_ctx->pkgs_ctx)) { - pkg_id = zhashx_cursor(group_ctx->pkgs_ctx); + for (pkg_ctx = (struct perf_group_pkg_context *) zhashx_first(group_ctx->pkgs_ctx); pkg_ctx; pkg_ctx = (struct perf_group_pkg_context *) zhashx_next(group_ctx->pkgs_ctx)) { + pkg_id = (const char *) zhashx_cursor(group_ctx->pkgs_ctx); pkg_data = payload_pkg_data_create(); if (!pkg_data) { zsys_error("perf<%s>: failed to allocate pkg data for group=%s pkg=%s", ctx->target_name, group_name, pkg_id); goto error; } - for (cpu_ctx = zhashx_first(pkg_ctx->cpus_ctx); cpu_ctx; cpu_ctx = zhashx_next(pkg_ctx->cpus_ctx)) { - cpu_id = zhashx_cursor(pkg_ctx->cpus_ctx); + for (cpu_ctx = (struct perf_group_cpu_context *) zhashx_first(pkg_ctx->cpus_ctx); cpu_ctx; cpu_ctx = (struct perf_group_cpu_context *) zhashx_next(pkg_ctx->cpus_ctx)) { + cpu_id = (const char *) zhashx_cursor(pkg_ctx->cpus_ctx); cpu_data = payload_cpu_data_create(); if (!cpu_data) { zsys_error("perf<%s>: failed to allocate cpu data for group=%s pkg=%s cpu=%s", ctx->target_name, group_name, pkg_id, cpu_id); @@ -450,16 +452,18 @@ populate_payload(struct perf_context *ctx, struct payload *payload) goto error; } +#if 0 /* warn if PMU multiplexing is happening */ perf_multiplexing_ratio = compute_perf_multiplexing_ratio(perf_read_buffer); if (perf_multiplexing_ratio < 1.0) { zsys_warning("perf<%s>: perf multiplexing for group=%s pkg=%s cpu=%s ratio=%f", ctx->target_name, group_name, pkg_id, cpu_id, perf_multiplexing_ratio); } +#endif /* store events value */ zhashx_insert(cpu_data->events, "time_enabled", &perf_read_buffer->time_enabled); zhashx_insert(cpu_data->events, "time_running", &perf_read_buffer->time_running); - for (event = zlistx_first(group_ctx->config->events), event_i = 0; event; event = zlistx_next(group_ctx->config->events), event_i++) { + for (event = (struct event_config *) zlistx_first(group_ctx->config->events), event_i = 0; event; event = (struct event_config *) zlistx_next(group_ctx->config->events), event_i++) { zhashx_insert(cpu_data->events, event->name, &perf_read_buffer->values[event_i].value); } @@ -512,7 +516,7 @@ handle_ticker(struct perf_context *ctx) void perf_monitoring_actor(zsock_t *pipe, void *args) { - struct perf_config *config = args; + struct perf_config *config = (struct perf_config *) args; char *target_name = NULL; struct perf_context *ctx = NULL; zsock_t *which = NULL; @@ -525,7 +529,7 @@ perf_monitoring_actor(zsock_t *pipe, void *args) goto cleanup; } - ctx = perf_context_create(args, pipe, target_name); + ctx = perf_context_create((struct perf_config *) args, pipe, target_name); if (!ctx) { zsys_error("perf<%s>: cannot create perf context", target_name); goto cleanup; @@ -541,7 +545,7 @@ perf_monitoring_actor(zsock_t *pipe, void *args) zsys_info("perf<%s>: monitoring actor started", target_name); while (!ctx->terminated) { - which = zpoller_wait(ctx->poller, -1); + which = (zsock_t *) zpoller_wait(ctx->poller, -1); if (zpoller_terminated(ctx->poller)) break; @@ -561,7 +565,7 @@ perf_monitoring_actor(zsock_t *pipe, void *args) int perf_try_global_counting_event_open(void) { - struct perf_event_attr pe = {0}; + struct perf_event_attr pe = {}; int fd; /* check support of perf_event by the kernel */ diff --git a/src/pmu.c b/src/pmu.c index b0ad1b0..375721e 100644 --- a/src/pmu.c +++ b/src/pmu.c @@ -54,7 +54,7 @@ pmu_deinitialize(void) struct pmu_info * pmu_info_create(void) { - struct pmu_info *pmu = malloc(sizeof(struct pmu_info)); + struct pmu_info *pmu = (struct pmu_info *) malloc(sizeof(struct pmu_info)); return pmu; } @@ -64,7 +64,7 @@ pmu_info_dup(struct pmu_info *pmu) struct pmu_info *copy = NULL; if (pmu) { - copy = malloc(sizeof(struct pmu_info)); + copy = (struct pmu_info *) malloc(sizeof(struct pmu_info)); if (copy) { *copy = *pmu; } @@ -85,7 +85,7 @@ pmu_info_destroy(struct pmu_info **pmu) struct pmu_topology * pmu_topology_create(void) { - struct pmu_topology *topology = malloc(sizeof(struct pmu_topology)); + struct pmu_topology *topology = (struct pmu_topology *) malloc(sizeof(struct pmu_topology)); if (!topology) return NULL; @@ -110,10 +110,10 @@ pmu_topology_destroy(struct pmu_topology *topology) int pmu_topology_detect(struct pmu_topology *topology) { - pfm_pmu_t pmu = {0}; - pfm_pmu_info_t pmu_info = {0}; + pfm_pmu_t pmu = {}; + pfm_pmu_info_t pmu_info = {}; - for (pmu = PFM_PMU_NONE; pmu < PFM_PMU_MAX; pmu++) { + for (pmu = PFM_PMU_NONE; pmu < PFM_PMU_MAX; pmu = static_cast(pmu + 1)) { if (pfm_get_pmu_info(pmu, &pmu_info) != PFM_SUCCESS) continue; diff --git a/src/report.c b/src/report.c index 09c3340..f430b90 100644 --- a/src/report.c +++ b/src/report.c @@ -43,7 +43,7 @@ struct report_config * report_config_create(struct storage_module *storage_module) { - struct report_config *config = malloc(sizeof(struct report_config)); + struct report_config *config = (struct report_config *) malloc(sizeof(struct report_config)); if (!config) return NULL; @@ -65,7 +65,7 @@ report_config_destroy(struct report_config *config) static struct report_context * report_context_create(struct report_config *config, zsock_t *pipe) { - struct report_context *ctx = malloc(sizeof(struct report_context)); + struct report_context *ctx = (struct report_context *) malloc(sizeof(struct report_context)); if (!ctx) return NULL; @@ -126,7 +126,7 @@ handle_reporting(struct report_context *ctx) void reporting_actor(zsock_t *pipe, void *args) { - struct report_context *ctx = report_context_create(args, pipe); + struct report_context *ctx = report_context_create((struct report_config *) args, pipe); zsock_t *which = NULL; if (!ctx) { @@ -137,7 +137,7 @@ reporting_actor(zsock_t *pipe, void *args) zsock_signal(pipe, 0); while (!ctx->terminated) { - which = zpoller_wait(ctx->poller, -1); + which = (zsock_t *) zpoller_wait(ctx->poller, -1); if (zpoller_terminated(ctx->poller)) { break; diff --git a/src/sensor.c b/src/sensor.c index dc36186..51b3a13 100644 --- a/src/sensor.c +++ b/src/sensor.c @@ -95,9 +95,9 @@ sync_cgroups_running_monitored(struct hwinfo *hwinfo, zhashx_t *container_events } /* stop monitoring dead container(s) */ - for (perf_monitor = zhashx_first(container_monitoring_actors); perf_monitor; perf_monitor = zhashx_next(container_monitoring_actors)) { - cgroup_path = zhashx_cursor(container_monitoring_actors); - target = zhashx_lookup(running_targets, cgroup_path); + for (perf_monitor = (zactor_t *) zhashx_first(container_monitoring_actors); perf_monitor; perf_monitor = (zactor_t *) zhashx_next(container_monitoring_actors)) { + cgroup_path = (const char *) zhashx_cursor(container_monitoring_actors); + target = (struct target *) zhashx_lookup(running_targets, cgroup_path); if (!target) { zhashx_freefn(running_targets, cgroup_path, (zhashx_free_fn *) target_destroy); zhashx_delete(container_monitoring_actors, cgroup_path); @@ -105,8 +105,8 @@ sync_cgroups_running_monitored(struct hwinfo *hwinfo, zhashx_t *container_events } /* start monitoring new container(s) */ - for (target = zhashx_first(running_targets); target; target = zhashx_next(running_targets)) { - cgroup_path = zhashx_cursor(running_targets); + for (target = (struct target *) zhashx_first(running_targets); target; target = (struct target *) zhashx_next(running_targets)) { + cgroup_path = (const char *) zhashx_cursor(running_targets); if (!zhashx_lookup(container_monitoring_actors, cgroup_path)) { monitor_config = perf_config_create(hwinfo, container_events_groups, target); perf_monitor = zactor_new(perf_monitoring_actor, monitor_config); @@ -130,7 +130,7 @@ main(int argc, char **argv) struct pmu_info *pmu = NULL; struct hwinfo *hwinfo = NULL; struct storage_module *storage = NULL; - struct report_config reporting_conf = {0}; + struct report_config reporting_conf = {}; zactor_t *reporting = NULL; zhashx_t *cgroups_running = NULL; /* char *cgroup_name -> char *cgroup_absolute_path */ zhashx_t *container_monitoring_actors = NULL; /* char *actor_name -> zactor_t *actor */ @@ -181,7 +181,7 @@ main(int argc, char **argv) zsys_error("pmu: cannot detect system PMU topology"); goto cleanup; } - for (pmu = zlistx_first(sys_pmu_topology->pmus); pmu; pmu = zlistx_next(sys_pmu_topology->pmus)) { + for (pmu = (struct pmu_info *) zlistx_first(sys_pmu_topology->pmus); pmu; pmu = (struct pmu_info *) zlistx_next(sys_pmu_topology->pmus)) { zsys_info("pmu: found %s '%s' having %d events, %d counters (%d general, %d fixed)", pmu->info.name, pmu->info.desc, diff --git a/src/storage_csv.c b/src/storage_csv.c index 8de1752..5ed320d 100644 --- a/src/storage_csv.c +++ b/src/storage_csv.c @@ -53,7 +53,7 @@ group_fd_destroy(FILE **fd_ptr) static struct csv_context * csv_context_create(const char *sensor_name, const char *output_dir) { - struct csv_context *ctx = malloc(sizeof(struct csv_context)); + struct csv_context *ctx = (struct csv_context *) malloc(sizeof(struct csv_context)); if (!ctx) return NULL; @@ -84,8 +84,8 @@ csv_context_destroy(struct csv_context *ctx) static int csv_initialize(struct storage_module *module) { - struct csv_context *ctx = module->context; - struct stat outdir_stat = {0}; + struct csv_context *ctx = (struct csv_context *) module->context; + struct stat outdir_stat = {}; /* create output directory */ errno = 0; @@ -129,7 +129,7 @@ csv_ping(struct storage_module *module __attribute__ ((unused))) static int write_group_header(struct csv_context *ctx, const char *group, FILE *fd, zhashx_t *events) { - char buffer[CSV_LINE_BUFFER_SIZE] = {0}; + char buffer[CSV_LINE_BUFFER_SIZE] = {}; int pos = 0; zlistx_t *events_name = NULL; const char *event_name = NULL; @@ -146,7 +146,7 @@ write_group_header(struct csv_context *ctx, const char *group, FILE *fd, zhashx_ pos += snprintf(buffer, CSV_LINE_BUFFER_SIZE, "timestamp,sensor,target,socket,cpu"); /* append dynamic elements (events) to buffer */ - for (event_name = zlistx_first(events_name); event_name; event_name = zlistx_next(events_name)) { + for (event_name = (const char * ) zlistx_first(events_name); event_name; event_name = (const char * ) zlistx_next(events_name)) { pos += snprintf(buffer + pos, CSV_LINE_BUFFER_SIZE - pos, ",%s", event_name); if (pos >= CSV_LINE_BUFFER_SIZE) goto error_buffer_too_small; @@ -172,7 +172,7 @@ write_group_header(struct csv_context *ctx, const char *group, FILE *fd, zhashx_ static int open_group_outfile(struct csv_context *ctx, const char *group_name) { - char path[PATH_MAX] = {0}; + char path[PATH_MAX] = {}; int fd = -1; FILE *file = NULL; @@ -204,13 +204,13 @@ static int write_events_value(struct csv_context *ctx, const char *group, FILE *fd, uint64_t timestamp, const char *target, const char *socket, const char *cpu, zhashx_t *events) { zlistx_t *events_name = NULL; - char buffer[CSV_LINE_BUFFER_SIZE] = {0}; + char buffer[CSV_LINE_BUFFER_SIZE] = {}; int pos = 0; const char *event_name = NULL; const uint64_t *event_value = NULL; /* get events name in the order of csv header */ - events_name = zhashx_lookup(ctx->groups_events, group); + events_name = (zlistx_t *) zhashx_lookup(ctx->groups_events, group); if (!events_name) return -1; @@ -218,8 +218,8 @@ write_events_value(struct csv_context *ctx, const char *group, FILE *fd, uint64_ pos += snprintf(buffer, CSV_LINE_BUFFER_SIZE, "%" PRIu64 ",%s,%s,%s,%s", timestamp, ctx->config.sensor_name, target, socket, cpu); /* write dynamic elements (events) to buffer */ - for (event_name = zlistx_first(events_name); event_name; event_name = zlistx_next(events_name)) { - event_value = zhashx_lookup(events, event_name); + for (event_name = (const char *) zlistx_first(events_name); event_name; event_name = (const char * ) zlistx_next(events_name)) { + event_value = (uint64_t *) zhashx_lookup(events, event_name); if (!event_value) return -1; @@ -237,7 +237,7 @@ write_events_value(struct csv_context *ctx, const char *group, FILE *fd, uint64_ static int csv_store_report(struct storage_module *module, struct payload *payload) { - struct csv_context *ctx = module->context; + struct csv_context *ctx = (struct csv_context *) module->context; struct payload_group_data *group_data = NULL; const char *group_name = NULL; FILE *group_fd = NULL; @@ -252,22 +252,22 @@ csv_store_report(struct storage_module *module, struct payload *payload) * timestamp,sensor,target,socket,cpu,INSTRUCTIONS_RETIRED,LLC_MISSES * 1538327257673,grvingt-64,system,0,56,5996,108 */ - for (group_data = zhashx_first(payload->groups); group_data; group_data = zhashx_next(payload->groups)) { - group_name = zhashx_cursor(payload->groups); - group_fd = zhashx_lookup(ctx->groups_fd, group_name); + for (group_data = (struct payload_group_data *) zhashx_first(payload->groups); group_data; group_data = (struct payload_group_data *) zhashx_next(payload->groups)) { + group_name = (const char *) zhashx_cursor(payload->groups); + group_fd = (FILE *) zhashx_lookup(ctx->groups_fd, group_name); if (!group_fd) { if (open_group_outfile(ctx, group_name)) return -1; - group_fd = zhashx_lookup(ctx->groups_fd, group_name); + group_fd = (FILE *) zhashx_lookup(ctx->groups_fd, group_name); write_header = true; } - for (pkg_data = zhashx_first(group_data->pkgs); pkg_data; pkg_data = zhashx_next(group_data->pkgs)) { - pkg_id = zhashx_cursor(group_data->pkgs); + for (pkg_data = (struct payload_pkg_data *) zhashx_first(group_data->pkgs); pkg_data; pkg_data = (struct payload_pkg_data *) zhashx_next(group_data->pkgs)) { + pkg_id = (const char *) zhashx_cursor(group_data->pkgs); - for (cpu_data = zhashx_first(pkg_data->cpus); cpu_data; cpu_data = zhashx_next(pkg_data->cpus)) { - cpu_id = zhashx_cursor(pkg_data->cpus); + for (cpu_data = (struct payload_cpu_data *) zhashx_first(pkg_data->cpus); cpu_data; cpu_data = (struct payload_cpu_data *) zhashx_next(pkg_data->cpus)) { + cpu_id = (const char *) zhashx_cursor(pkg_data->cpus); if (write_header) { if (write_group_header(ctx, group_name, group_fd, cpu_data->events)) { @@ -300,7 +300,7 @@ csv_destroy(struct storage_module *module) if (!module) return; - csv_context_destroy(module->context); + csv_context_destroy((csv_context *) module->context); } struct storage_module * @@ -309,7 +309,7 @@ storage_csv_create(struct config *config) struct storage_module *module = NULL; struct csv_context *ctx = NULL; - module = malloc(sizeof(struct storage_module)); + module = (struct storage_module *) malloc(sizeof(struct storage_module)); if (!module) goto error; diff --git a/src/storage_mongodb.c b/src/storage_mongodb.c index 1a7b1bd..998c255 100644 --- a/src/storage_mongodb.c +++ b/src/storage_mongodb.c @@ -38,7 +38,7 @@ static struct mongodb_context * mongodb_context_create(const char *sensor_name, const char *uri, const char *database, const char *collection) { - struct mongodb_context *ctx = malloc(sizeof(struct mongodb_context)); + struct mongodb_context *ctx = (struct mongodb_context *) malloc(sizeof(struct mongodb_context)); if (!ctx) return NULL; @@ -66,7 +66,7 @@ mongodb_context_destroy(struct mongodb_context *ctx) static int mongodb_initialize(struct storage_module *module) { - struct mongodb_context *ctx = module->context; + struct mongodb_context *ctx = (struct mongodb_context *) module->context; bson_error_t error; if (module->is_initialized) @@ -101,7 +101,7 @@ mongodb_initialize(struct storage_module *module) static int mongodb_ping(struct storage_module *module) { - struct mongodb_context *ctx = module->context; + struct mongodb_context *ctx = (struct mongodb_context *) module->context; int ret = 0; bson_t *ping_cmd = BCON_NEW("ping", BCON_INT32(1)); bson_error_t error; @@ -118,7 +118,7 @@ mongodb_ping(struct storage_module *module) static int mongodb_store_report(struct storage_module *module, struct payload *payload) { - struct mongodb_context *ctx = module->context; + struct mongodb_context *ctx = (struct mongodb_context *) module->context; bson_t document = BSON_INITIALIZER; bson_t doc_groups; struct payload_group_data *group_data = NULL; @@ -163,20 +163,20 @@ mongodb_store_report(struct storage_module *module, struct payload *payload) BSON_APPEND_UTF8(&document, "target", payload->target_name); BSON_APPEND_DOCUMENT_BEGIN(&document, "groups", &doc_groups); - for (group_data = zhashx_first(payload->groups); group_data; group_data = zhashx_next(payload->groups)) { - group_name = zhashx_cursor(payload->groups); + for (group_data = (struct payload_group_data *) zhashx_first(payload->groups); group_data; group_data = (struct payload_group_data *) zhashx_next(payload->groups)) { + group_name = (const char *) zhashx_cursor(payload->groups); BSON_APPEND_DOCUMENT_BEGIN(&doc_groups, group_name, &doc_group); - for (pkg_data = zhashx_first(group_data->pkgs); pkg_data; pkg_data = zhashx_next(group_data->pkgs)) { - pkg_id = zhashx_cursor(group_data->pkgs); + for (pkg_data = (struct payload_pkg_data *) zhashx_first(group_data->pkgs); pkg_data; pkg_data = (struct payload_pkg_data *) zhashx_next(group_data->pkgs)) { + pkg_id = (const char * ) zhashx_cursor(group_data->pkgs); BSON_APPEND_DOCUMENT_BEGIN(&doc_group, pkg_id, &doc_pkg); - for (cpu_data = zhashx_first(pkg_data->cpus); cpu_data; cpu_data = zhashx_next(pkg_data->cpus)) { - cpu_id = zhashx_cursor(pkg_data->cpus); + for (cpu_data = (struct payload_cpu_data *) zhashx_first(pkg_data->cpus); cpu_data; cpu_data = (struct payload_cpu_data *) zhashx_next(pkg_data->cpus)) { + cpu_id = (const char *) zhashx_cursor(pkg_data->cpus); BSON_APPEND_DOCUMENT_BEGIN(&doc_pkg, cpu_id, &doc_cpu); - for (event_value = zhashx_first(cpu_data->events); event_value; event_value = zhashx_next(cpu_data->events)) { - event_name = zhashx_cursor(cpu_data->events); + for (event_value = (uint64_t *) zhashx_first(cpu_data->events); event_value; event_value = (uint64_t *) zhashx_next(cpu_data->events)) { + event_name = (const char *) zhashx_cursor(cpu_data->events); BSON_APPEND_DOUBLE(&doc_cpu, event_name, *event_value); } @@ -203,7 +203,7 @@ mongodb_store_report(struct storage_module *module, struct payload *payload) static int mongodb_deinitialize(struct storage_module *module __attribute__ ((unused))) { - struct mongodb_context *ctx = module->context; + struct mongodb_context *ctx = (struct mongodb_context *) module->context; if (!module->is_initialized) return -1; @@ -222,7 +222,7 @@ mongodb_destroy(struct storage_module *module) if (!module) return; - mongodb_context_destroy(module->context); + mongodb_context_destroy((struct mongodb_context *) module->context); } struct storage_module * @@ -231,7 +231,7 @@ storage_mongodb_create(struct config *config) struct storage_module *module = NULL; struct mongodb_context *ctx = NULL; - module = malloc(sizeof(struct storage_module)); + module = (struct storage_module *) malloc(sizeof(struct storage_module)); if (!module) goto error; diff --git a/src/storage_null.c b/src/storage_null.c index 9f164c5..cce0a41 100644 --- a/src/storage_null.c +++ b/src/storage_null.c @@ -70,7 +70,7 @@ null_destroy(struct storage_module *module __attribute__ ((unused))) struct storage_module * storage_null_create(struct config *config __attribute__ ((unused))) { - struct storage_module *module = malloc(sizeof(struct storage_module)); + struct storage_module *module = (struct storage_module *) malloc(sizeof(struct storage_module)); if (!module) return NULL; diff --git a/src/storage_socket.c b/src/storage_socket.c index 54aef8f..127797d 100644 --- a/src/storage_socket.c +++ b/src/storage_socket.c @@ -45,7 +45,7 @@ static struct socket_context * socket_context_create(const char *sensor_name, const char *address, const char *port) { - struct socket_context *ctx = malloc(sizeof(struct socket_context)); + struct socket_context *ctx = (struct socket_context *) malloc(sizeof(struct socket_context)); if (!ctx) return NULL; @@ -73,7 +73,7 @@ socket_context_destroy(struct socket_context *ctx) static int socket_resolve_and_connect(struct socket_context *ctx) { - struct addrinfo hints = {0}; + struct addrinfo hints = {}; struct addrinfo *result = NULL, *rp = NULL; int sfd = -1; int ret = -1; @@ -119,7 +119,7 @@ socket_resolve_and_connect(struct socket_context *ctx) static int socket_initialize(struct storage_module *module) { - struct socket_context *ctx = module->context; + struct socket_context *ctx = (struct socket_context *) module->context; if (module->is_initialized) return -1; @@ -177,7 +177,7 @@ socket_try_reconnect(struct socket_context *ctx) static int socket_store_report(struct storage_module *module, struct payload *payload) { - struct socket_context *ctx = module->context; + struct socket_context *ctx = (struct socket_context *) module->context; struct json_object *jobj = NULL; struct json_object *jobj_groups = NULL; struct payload_group_data *group_data = NULL; @@ -193,7 +193,7 @@ socket_store_report(struct storage_module *module, struct payload *payload) uint64_t *event_value = NULL; const char *json_report = NULL; size_t json_report_length = 0; - struct iovec socket_iov[2] = {0}; + struct iovec socket_iov[2] = {}; ssize_t nbsend; int retry_once = 1; int ret = -1; @@ -234,23 +234,23 @@ socket_store_report(struct storage_module *module, struct payload *payload) jobj_groups = json_object_new_object(); json_object_object_add(jobj, "groups", jobj_groups); - for (group_data = zhashx_first(payload->groups); group_data; group_data = zhashx_next(payload->groups)) { + for (group_data = (struct payload_group_data *) zhashx_first(payload->groups); group_data; group_data = (struct payload_group_data *) zhashx_next(payload->groups)) { jobj_group = json_object_new_object(); - group_name = zhashx_cursor(payload->groups); + group_name = (const char *) zhashx_cursor(payload->groups); json_object_object_add(jobj_groups, group_name, jobj_group); - for (pkg_data = zhashx_first(group_data->pkgs); pkg_data; pkg_data = zhashx_next(group_data->pkgs)) { + for (pkg_data = (struct payload_pkg_data *) zhashx_first(group_data->pkgs); pkg_data; pkg_data = (struct payload_pkg_data *) zhashx_next(group_data->pkgs)) { jobj_pkg = json_object_new_object(); - pkg_id = zhashx_cursor(group_data->pkgs); + pkg_id = (const char *) zhashx_cursor(group_data->pkgs); json_object_object_add(jobj_group, pkg_id, jobj_pkg); - for (cpu_data = zhashx_first(pkg_data->cpus); cpu_data; cpu_data = zhashx_next(pkg_data->cpus)) { + for (cpu_data = (struct payload_cpu_data *) zhashx_first(pkg_data->cpus); cpu_data; cpu_data = (struct payload_cpu_data *) zhashx_next(pkg_data->cpus)) { jobj_cpu = json_object_new_object(); - cpu_id = zhashx_cursor(pkg_data->cpus); + cpu_id = (const char *) zhashx_cursor(pkg_data->cpus); json_object_object_add(jobj_pkg, cpu_id, jobj_cpu); - for (event_value = zhashx_first(cpu_data->events); event_value; event_value = zhashx_next(cpu_data->events)) { - event_name = zhashx_cursor(cpu_data->events); + for (event_value = (uint64_t *) zhashx_first(cpu_data->events); event_value; event_value = (uint64_t *) zhashx_next(cpu_data->events)) { + event_name = (const char *) zhashx_cursor(cpu_data->events); json_object_object_add(jobj_cpu, event_name, json_object_new_uint64(*event_value)); } } @@ -269,7 +269,7 @@ socket_store_report(struct storage_module *module, struct payload *payload) */ socket_iov[0].iov_base = (void *) json_report; socket_iov[0].iov_len = json_report_length; - socket_iov[1].iov_base = "\n"; + socket_iov[1].iov_base = (void *) "\n"; socket_iov[1].iov_len = 1; do { @@ -304,7 +304,7 @@ socket_store_report(struct storage_module *module, struct payload *payload) static int socket_deinitialize(struct storage_module *module) { - struct socket_context *ctx = module->context; + struct socket_context *ctx = (struct socket_context *) module->context; if (!module->is_initialized) return -1; @@ -321,7 +321,7 @@ socket_destroy(struct storage_module *module) if (!module) return; - socket_context_destroy(module->context); + socket_context_destroy((struct socket_context *) module->context); } struct storage_module * @@ -330,7 +330,7 @@ storage_socket_create(struct config *config) struct storage_module *module = NULL; struct socket_context *ctx = NULL; - module = malloc(sizeof(struct storage_module)); + module = (struct storage_module *) malloc(sizeof(struct storage_module)); if (!module) goto error; diff --git a/src/target.c b/src/target.c index 27fc60a..2ea05dd 100644 --- a/src/target.c +++ b/src/target.c @@ -41,16 +41,6 @@ #include "target_docker.h" #include "target_kubernetes.h" -const char *target_types_name[] = { - [TARGET_TYPE_UNKNOWN] = "unknown", - [TARGET_TYPE_ALL] = "all", - [TARGET_TYPE_SYSTEM] = "system", - [TARGET_TYPE_KERNEL] = "kernel", - [TARGET_TYPE_DOCKER] = "docker", - [TARGET_TYPE_KUBERNETES] = "k8s", - [TARGET_TYPE_LIBVIRT] = "libvirt", - [TARGET_TYPE_LXC] = "lxc", -}; enum target_type target_detect_type(const char *cgroup_path) @@ -105,7 +95,7 @@ target_validate_type(enum target_type type, const char *cgroup_path) struct target * target_create(enum target_type type, const char *cgroup_basedir, const char *cgroup_path) { - struct target *target = malloc(sizeof(struct target)); + struct target *target = (struct target *) malloc(sizeof(struct target)); if (!target) return NULL; @@ -132,10 +122,15 @@ target_resolve_real_name(struct target *target) break; case TARGET_TYPE_ALL: + target_real_name = strdup("all"); + break; + case TARGET_TYPE_KERNEL: + target_real_name = strdup("kernel"); + break; + case TARGET_TYPE_SYSTEM: - /* the above types have static name */ - target_real_name = strdup(target_types_name[target->type]); + target_real_name = strdup("system"); break; default: diff --git a/src/target.h b/src/target.h index d32e5e0..f30d235 100644 --- a/src/target.h +++ b/src/target.h @@ -50,11 +50,6 @@ enum target_type TARGET_TYPE_EVERYTHING = 255 }; -/* - * target_types_name stores the name (as string) of the supported target types. - */ -extern const char *target_types_name[]; - /* * target stores various information about the target. */ diff --git a/src/target_docker.c b/src/target_docker.c index 1ad64f9..5a503e6 100644 --- a/src/target_docker.c +++ b/src/target_docker.c @@ -79,7 +79,7 @@ build_container_config_path(const char *cgroup_path) regmatch_t matches[CONTAINER_ID_REGEX_EXPECTED_MATCHES]; regoff_t length; const char *id = NULL; - char buffer[PATH_MAX] = {0}; + char buffer[PATH_MAX] = {}; char *config_path = NULL; if (!regcomp(&re, CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE)) { diff --git a/src/target_kubernetes.c b/src/target_kubernetes.c index c8e771e..2a6bc62 100644 --- a/src/target_kubernetes.c +++ b/src/target_kubernetes.c @@ -81,7 +81,7 @@ build_container_config_path(const char *cgroup_path) regmatch_t matches[CONTAINER_ID_REGEX_EXPECTED_MATCHES]; regoff_t length; const char *id = NULL; - char buffer[PATH_MAX] = {0}; + char buffer[PATH_MAX] = {}; char *config_path = NULL; if (!regcomp(&re, CONTAINER_ID_REGEX, REG_EXTENDED | REG_NEWLINE)) { diff --git a/src/util.c b/src/util.c index 2584560..5b04f0a 100644 --- a/src/util.c +++ b/src/util.c @@ -39,7 +39,7 @@ int * intdup(int val) { - int *res = malloc(sizeof(int)); + int *res = (int *) malloc(sizeof(int)); if (!res) return NULL; @@ -73,7 +73,7 @@ intptrcmp(const int *a, const int *b) uint64_t * uint64dup(uint64_t val) { - uint64_t *res = malloc(sizeof(uint64_t)); + uint64_t *res = (uint64_t *) malloc(sizeof(uint64_t)); if (!res) return NULL; From 1336dbae969e62473751815ba091c9d8167143da Mon Sep 17 00:00:00 2001 From: Guillaume Fieni Date: Wed, 7 Feb 2024 21:57:06 +0200 Subject: [PATCH 2/2] build(CMake): Update config to compile the project as C++ --- CMakeLists.txt | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 089f9c3..e031275 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,18 +1,24 @@ cmake_minimum_required(VERSION 3.13) -project(hwpc-sensor LANGUAGES C) +project(hwpc-sensor LANGUAGES CXX) option(WITH_MONGODB "Build with support for MongoDB storage module" ON) -set(CMAKE_C_STANDARD 11) set(CMAKE_EXPORT_COMPILE_COMMANDS ON) -set(CMAKE_POSITION_INDEPENDENT_CODE ON) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") -add_compile_options(-Werror -Wall -Wextra -Wpedantic -Wformat=2 -Wnull-dereference -Wno-gnu-statement-expression) -add_link_options(-Wl,-z,relro,-z,now -pie) -set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -Og -fsanitize=address,undefined -fno-omit-frame-pointer") -set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -D_FORTIFY_SOURCE=2 -fstack-protector-strong") +add_compile_options(-Werror -Wall -Wextra -Wformat=2 -fstack-protector-strong -D_FORTIFY_SOURCE=2 -fPIE) +add_link_options(-pie -Wl,-z,relro,-z,now -Wl,-z,noexecstack -Wl,-z,defs -Wl,--as-needed) + +if (CMAKE_CXX_COMPILER_ID MATCHES "Clang") + # These warnings need to be suppressed temporarily, as the only fix is to rewrite the code in C++ + add_compile_options(-Wno-deprecated -Wno-c99-designator -Wno-vla-cxx-extension) +endif() + +if (CMAKE_BUILD_TYPE STREQUAL "Debug" AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang") + add_compile_options(-Og -fsanitize=address,undefined -fno-omit-frame-pointer) + add_link_options(-fsanitize=address,undefined) +endif() set(SENSOR_SOURCES src/config_cli.c @@ -51,5 +57,13 @@ if(DEFINED ENV{GIT_TAG} AND DEFINED ENV{GIT_REV}) endif() add_executable(hwpc-sensor "${SENSOR_SOURCES}") + +foreach(src ${SENSOR_SOURCES}) + set_source_files_properties(${src} PROPERTIES LANGUAGE CXX) +endforeach() + +target_compile_features(hwpc-sensor PUBLIC cxx_std_20) +set_target_properties(hwpc-sensor PROPERTIES CXX_EXTENSIONS OFF LINKER_LANGUAGE CXX) + target_include_directories(hwpc-sensor SYSTEM PRIVATE "${LIBPFM_INCLUDE_DIRS}" "${CZMQ_INCLUDE_DIRS}" "${JSONC_INCLUDE_DIRS}" "${MONGOC_INCLUDE_DIRS}") target_link_libraries(hwpc-sensor "${LIBPFM_LIBRARIES}" "${CZMQ_LIBRARIES}" "${JSONC_LIBRARIES}" "${MONGOC_LIBRARIES}")