Tests: Fix abi conflict test when building with clang
[lttng-ust.git] / src / common / counter / counter.c
index 60edad0c5b63b9ea2a1f3f92e9ca42e5ab400217..250701fccbcda26ef81e57ab2569f32cdf46a256 100644 (file)
@@ -17,6 +17,7 @@
 #include "common/bitmap.h"
 
 #include "common/smp.h"
+#include "common/populate.h"
 #include "shm.h"
 
 static size_t lttng_counter_get_dimension_nr_elements(struct lib_counter_dimension *dimension)
@@ -84,13 +85,14 @@ static int lttng_counter_layout_init(struct lib_counter *counter, int cpu, int s
        if (counter->is_daemon) {
                /* Allocate and clear shared memory. */
                shm_object = lttng_counter_shm_object_table_alloc(counter->object_table,
-                       shm_length, LTTNG_COUNTER_SHM_OBJECT_SHM, shm_fd, cpu);
+                       shm_length, LTTNG_COUNTER_SHM_OBJECT_SHM, shm_fd, cpu,
+                       lttng_ust_map_populate_cpu_is_enabled(cpu));
                if (!shm_object)
                        return -ENOMEM;
        } else {
                /* Map pre-existing shared memory. */
                shm_object = lttng_counter_shm_object_table_append_shm(counter->object_table,
-                       shm_fd, shm_length);
+                       shm_fd, shm_length, lttng_ust_map_populate_cpu_is_enabled(cpu));
                if (!shm_object)
                        return -ENOMEM;
        }
@@ -104,19 +106,24 @@ int lttng_counter_set_global_shm(struct lib_counter *counter, int fd)
 {
        struct lib_counter_config *config = &counter->config;
        struct lib_counter_layout *layout;
+       int ret;
 
        if (!(config->alloc & COUNTER_ALLOC_GLOBAL))
                return -EINVAL;
        layout = &counter->global_counters;
        if (layout->shm_fd >= 0)
                return -EBUSY;
-       return lttng_counter_layout_init(counter, -1, fd);
+       ret = lttng_counter_layout_init(counter, -1, fd);
+       if (!ret)
+               counter->received_shm++;
+       return ret;
 }
 
 int lttng_counter_set_cpu_shm(struct lib_counter *counter, int cpu, int fd)
 {
        struct lib_counter_config *config = &counter->config;
        struct lib_counter_layout *layout;
+       int ret;
 
        if (cpu < 0 || cpu >= get_possible_cpus_array_len())
                return -EINVAL;
@@ -126,7 +133,10 @@ int lttng_counter_set_cpu_shm(struct lib_counter *counter, int cpu, int fd)
        layout = &counter->percpu_counters[cpu];
        if (layout->shm_fd >= 0)
                return -EBUSY;
-       return lttng_counter_layout_init(counter, cpu, fd);
+       ret = lttng_counter_layout_init(counter, cpu, fd);
+       if (!ret)
+               counter->received_shm++;
+       return ret;
 }
 
 static
@@ -211,12 +221,13 @@ struct lib_counter *lttng_counter_create(const struct lib_counter_config *config
        int cpu, ret;
        int nr_handles = 0;
        int nr_cpus = get_possible_cpus_array_len();
+       bool populate = lttng_ust_map_populate_is_enabled();
 
        if (validate_args(config, nr_dimensions, max_nr_elem,
                        global_sum_step, global_counter_fd, nr_counter_cpu_fds,
                        counter_cpu_fds))
                return NULL;
-       counter = zmalloc(sizeof(struct lib_counter));
+       counter = zmalloc_populate(sizeof(struct lib_counter), populate);
        if (!counter)
                return NULL;
        counter->global_counters.shm_fd = -1;
@@ -225,13 +236,13 @@ struct lib_counter *lttng_counter_create(const struct lib_counter_config *config
        if (lttng_counter_set_global_sum_step(counter, global_sum_step))
                goto error_sum_step;
        counter->nr_dimensions = nr_dimensions;
-       counter->dimensions = zmalloc(nr_dimensions * sizeof(*counter->dimensions));
+       counter->dimensions = zmalloc_populate(nr_dimensions * sizeof(*counter->dimensions), populate);
        if (!counter->dimensions)
                goto error_dimensions;
        for (dimension = 0; dimension < nr_dimensions; dimension++)
                counter->dimensions[dimension].max_nr_elem = max_nr_elem[dimension];
        if (config->alloc & COUNTER_ALLOC_PER_CPU) {
-               counter->percpu_counters = zmalloc(sizeof(struct lib_counter_layout) * nr_cpus);
+               counter->percpu_counters = zmalloc_populate(sizeof(struct lib_counter_layout) * nr_cpus, populate);
                if (!counter->percpu_counters)
                        goto error_alloc_percpu;
                for_each_possible_cpu(cpu)
@@ -249,8 +260,9 @@ struct lib_counter *lttng_counter_create(const struct lib_counter_config *config
                nr_handles++;
        if (config->alloc & COUNTER_ALLOC_PER_CPU)
                nr_handles += nr_cpus;
+       counter->expected_shm = nr_handles;
        /* Allocate table for global and per-cpu counters. */
-       counter->object_table = lttng_counter_shm_object_table_create(nr_handles);
+       counter->object_table = lttng_counter_shm_object_table_create(nr_handles, populate);
        if (!counter->object_table)
                goto error_alloc_object_table;
 
@@ -320,6 +332,13 @@ int lttng_counter_get_cpu_shm(struct lib_counter *counter, int cpu, int *fd, siz
        return 0;
 }
 
+bool lttng_counter_ready(struct lib_counter *counter)
+{
+       if (counter->received_shm == counter->expected_shm)
+               return true;
+       return false;
+}
+
 int lttng_counter_read(const struct lib_counter_config *config,
                       struct lib_counter *counter,
                       const size_t *dimension_indexes,
@@ -450,6 +469,35 @@ int lttng_counter_aggregate(const struct lib_counter_config *config,
        default:
                return -EINVAL;
        }
+       switch (config->counter_size) {
+       case COUNTER_SIZE_8_BIT:
+               if (sum > INT8_MAX)
+                       *overflow = true;
+               if (sum < INT8_MIN)
+                       *underflow = true;
+               sum = (int8_t) sum;     /* Truncate sum. */
+               break;
+       case COUNTER_SIZE_16_BIT:
+               if (sum > INT16_MAX)
+                       *overflow = true;
+               if (sum < INT16_MIN)
+                       *underflow = true;
+               sum = (int16_t) sum;    /* Truncate sum. */
+               break;
+       case COUNTER_SIZE_32_BIT:
+               if (sum > INT32_MAX)
+                       *overflow = true;
+               if (sum < INT32_MIN)
+                       *underflow = true;
+               sum = (int32_t) sum;    /* Truncate sum. */
+               break;
+#if CAA_BITS_PER_LONG == 64
+       case COUNTER_SIZE_64_BIT:
+               break;
+#endif
+       default:
+               return -EINVAL;
+       }
        *value = sum;
        return 0;
 }
This page took 0.025968 seconds and 4 git commands to generate.