diff --git a/makefile b/makefile index 23c02dc6d9dc9dfba711565ad5eef2707aaa2934..a383614961e467e5561d149f973f1c39afcc050c 100644 --- a/makefile +++ b/makefile @@ -1,6 +1,6 @@ all: mojitos -OBJECTS = mojitos.o counters_individual.o rapl.o network.o load.o infiniband.o +OBJECTS = mojitos.o counters_individual.o rapl.o network.o load.o infiniband.o temperature.o mojitos:$(OBJECTS) counters_option.h gcc $(DEBUG) -O3 -Wall -o mojitos $(OBJECTS) -lpowercap diff --git a/mojitos.c b/mojitos.c index b72e2b72db770f25b4e47189ed924a66423fccb2..c8bbd92f58bf18ada6a4a502d196ad6a5cb6c157 100644 --- a/mojitos.c +++ b/mojitos.c @@ -30,7 +30,7 @@ #include "network.h" #include "infiniband.h" #include "load.h" - +#include "temperature.h" @@ -111,7 +111,7 @@ int main(int argc, char **argv) { signal(15, flush); int c; - while ((c = getopt (argc, argv, "ilhftdeoprsu")) != -1 && application==NULL) + while ((c = getopt (argc, argv, "ilhcftdeoprsu")) != -1 && application==NULL) switch (c) { case 'f': frequency=atoi(argv[optind]); @@ -146,6 +146,9 @@ int main(int argc, char **argv) { case 'u': add_source(init_load, NULL, label_load, get_load, clean_load); break; + case 'c': + add_source(init_temperature, NULL, label_temperature, get_temperature, clean_temperature); + break; case 's': stat_mode=0; break; @@ -155,7 +158,8 @@ int main(int argc, char **argv) { default: usage(argv); } - + + struct timespec ts; struct timespec ts_ref; diff --git a/temperature.c b/temperature.c new file mode 100644 index 0000000000000000000000000000000000000000..142e86ae61604d920c92563196cffef9ad043386 --- /dev/null +++ b/temperature.c @@ -0,0 +1,137 @@ +/******************************************************* + Copyright (C) 2018-2021 Georges Da Costa <georges.da-costa@irit.fr> + + This file is part of Mojitos. + + Mojitos is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Mojitos is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MojitO/S. If not, see <https://www.gnu.org/licenses/>. + + *******************************************************/ + +#include <stdlib.h> +#include <unistd.h> +#include <fcntl.h> +#include <string.h> +#include <stdio.h> +#include <stdint.h> + +struct temperature_t { + char **label_list; + int *fid_list; + int nb_elem; +}; + +int get_string(char *filename, char *buffer, int max_size) { + int fid = open(filename, O_RDONLY); + //printf("Tries to open : %s : %d\n", filename, fid); + if(fid == -1) + return -1; + + int nb = read(fid, buffer, max_size); + if(nb == -1) { + close(fid); + return -1; + } + + buffer[nb]=0; + close(fid); + return 0; +} + +void add_to_list(char ***list_name, char *source, int nb_elem) { + //printf("Adds: %s\n", source); + *list_name = realloc(*list_name, (nb_elem+1)*sizeof(char*)); + (*list_name)[nb_elem] = malloc(strlen(source)+1); + strcpy((*list_name)[nb_elem], source); + +} + +void add_temperature_sensor(int id_rep, struct temperature_t *state) { + static int key=0; + static char buffer_filename[512]; + static char buffer_label[512]; + + int delta = sprintf(buffer_label, "Temp_%d_", key); + + for(int i=1;;i++) { + sprintf(buffer_filename, "/sys/class/hwmon/hwmon%d/temp%d_label", id_rep, i); + if(get_string(buffer_filename, buffer_label+delta, 100) == -1) + break; + + for(int pos = 0; pos < strlen(buffer_label); pos++) { + if (buffer_label[pos] == ' ') buffer_label[pos] = '_'; + if (buffer_label[pos] == '\n') buffer_label[pos] = '\0'; + } + add_to_list(&state->label_list, buffer_label, state->nb_elem); + + sprintf(buffer_filename, "/sys/class/hwmon/hwmon%d/temp%d_input", id_rep, i); + state->fid_list = realloc(state->fid_list, (state->nb_elem+1)*sizeof(int)); + state->fid_list[state->nb_elem] = open(buffer_filename, O_RDONLY); + + state->nb_elem++; + // printf("%s : %s\n", buffer_label, buffer_filename); + } + + key++; +} + +unsigned int init_temperature(char *args, void** ptr) { + struct temperature_t *state = malloc(sizeof(struct temperature_t)); + state->nb_elem = 0; + state->label_list = NULL; + state->fid_list = NULL; + + char base_name[] = "/sys/class/hwmon/hwmon%d/name"; + static char name[512]; + static char buffer[512]; + + int i = 0; + sprintf(name, base_name, i); + while(get_string(name, buffer, 8) != -1) { + if (strcmp(buffer, "coretemp")==0) + add_temperature_sensor(i, state); + + i++; + sprintf(name, base_name, i); + } + *ptr = (void*) state; + return state->nb_elem; +} + +unsigned int get_temperature(uint64_t* results, void* ptr) { + struct temperature_t *state = (struct temperature_t*)ptr; + static char buffer[512]; + for(int i=0; i<state->nb_elem; i++) { + pread(state->fid_list[i], buffer, 100, 0); + results[i] = strtoull(buffer, NULL, 10); + } + return state->nb_elem; +} + +void clean_temperature(void* ptr) { + struct temperature_t *state = (struct temperature_t*)ptr; + + for(int i=0; i<state->nb_elem; i++) { + free(state->label_list[i]); + close(state->fid_list[i]); + } + free(state->label_list); + free(state->fid_list); + free(state); +} + +void label_temperature(char**labels, void* ptr) { + struct temperature_t *state = (struct temperature_t*)ptr; + for(int i=0; i<state->nb_elem; i++) + labels[i] = state->label_list[i]; +} diff --git a/temperature.h b/temperature.h new file mode 100644 index 0000000000000000000000000000000000000000..c6c34df6c612e709764e2b7d9dfd37ec234ccbf5 --- /dev/null +++ b/temperature.h @@ -0,0 +1,24 @@ +/******************************************************* + Copyright (C) 2018-2021 Georges Da Costa <georges.da-costa@irit.fr> + + This file is part of Mojitos. + + Mojitos is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + Mojitos is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with MojitO/S. If not, see <https://www.gnu.org/licenses/>. + + *******************************************************/ + +unsigned int init_temperature(char*, void **); +unsigned int get_temperature(uint64_t* results, void*); +void clean_temperature(void *); +void label_temperature(char **labels, void*);