Skip to content
Snippets Groups Projects
Commit 10d6b8c4 authored by ghuter's avatar ghuter
Browse files

fix sensor example (doc)

plus add build instructions (in doc/counter_ex.c)
parent 9026cbb2
No related branches found
No related tags found
4 merge requests!9fix sensor example (doc),!4Amd, Tests, Doc, CI/CD,!2build system (v0), add long options,!1build system (v0), add long options, corrections
/*
* compilation:
*
# normally, this part is done with `configure.sh`, but here we need to
# do this manually
cat <<EOF > src/sensors.h
#include "counter_ex.h" #include "counter_ex.h"
static int acc; #define NB_SENSOR 1
#define NB_SENSOR_OPT 1
void init_sensors(Optparse *opts, Sensor *sensors, size_t len, size_t offset, int *nb_defined)
{
int opt_idx = offset;
for (int i = 0; i < counter_ex.nb_opt; i++) {
opts[opt_idx++] = counter_ex_opt[i];
}
sensors[(*nb_defined)++] = counter_ex;
assert((offset + *nb_defined) <= len);
}
EOF
# idem
echo 'CAPTOR_OBJ = doc/counters_ex.o' > sensors.mk
# actual compilation here
gcc -std=gnu99 -Wall -Wpedantic -I./lib -I./doc -I./src -g -Og -c doc/counter_ex.c -o obj/counter_ex.o
gcc -std=gnu99 -Wall -Wpedantic -I./lib -I./doc -I./src -g -Og -c doc/util.c -o obj/util.o
gcc -std=gnu99 -Wall -Wpedantic -I./lib -I./doc -I./src -g -Og -c doc/mojitos.c -o obj/mojitos.o
gcc -std=gnu99 -Wall -Wpedantic -I./lib -I./doc -I./src -g -Og obj/util.o obj/mojitos.o obj/counter_ex.o -o bin/mojitos
unsigned int init_acc(char *a, void **b) *
**/
#include <stdint.h>
#include <stdlib.h>
#include "util.h"
#define NB_SENSOR 3
struct accumulator_t {
int v[NB_SENSOR];
};
void _get_acc(int v[NB_SENSOR])
{ {
acc = 0; for (int i = 0; i < NB_SENSOR; i++) {
v[i]++;
}
} }
unsigned int get_acc(uint64_t *results, void *none) unsigned int init_acc(char *none, void **ptr)
{ {
/* "none" refers to an optionnal command-line argument */
/* there is none in this case, so this parameter is not used */
UNUSED(none); UNUSED(none);
return a++;
struct accumulator_t *state = malloc(sizeof(struct accumulator_t));
for (int i = 0; i < NB_SENSOR; i++) {
state->v[i] = -1;
}
*ptr = (void *)state;
_get_acc(state->v);
return NB_SENSOR;
}
unsigned int get_acc(uint64_t *results, void *ptr)
{
struct accumulator_t *state = (struct accumulator_t *)ptr;
_get_acc(state->v);
for (int i = 0; i < NB_SENSOR; i++) {
results[i] = state->v[i];
}
return NB_SENSOR;
} }
char *_labels_accumulator[NB_SENSOR] = {"acc1", "acc2", "acc3"};
void label_acc(char **labels, void *none) void label_acc(char **labels, void *none)
{ {
UNUSED(none); UNUSED(none);
labels[0] = "acc"; for (int i = 0; i < NB_SENSOR; i++) {
labels[i] = _labels_accumulator[i];
}
} }
void clean_acc(void *none) void clean_acc(void *ptr)
{ {
/* That's a no-op for this example */ struct accumulator_t *state = (struct accumulator_t *)ptr;
UNUSED(none);
if (state == NULL) {
return;
}
free(state);
} }
...@@ -7,7 +7,7 @@ unsigned int get_acc(uint64_t *results, void *); ...@@ -7,7 +7,7 @@ unsigned int get_acc(uint64_t *results, void *);
void clean_acc(void *); void clean_acc(void *);
void label_acc(char **labels, void *); void label_acc(char **labels, void *);
Sensor rapl = { Sensor counter_ex = {
.init = init_acc, .init = init_acc,
.get = get_acc, .get = get_acc,
.clean = clean_acc, .clean = clean_acc,
...@@ -15,7 +15,7 @@ Sensor rapl = { ...@@ -15,7 +15,7 @@ Sensor rapl = {
.nb_opt = 1, .nb_opt = 1,
}; };
Optparse rapl_opt[1] = { Optparse counter_ex_opt[1] = {
{ {
.longname = "accumulator", .longname = "accumulator",
.shortname = 'a', .shortname = 'a',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment