Add logs for chunk_array improvement
							parent
							
								
									3c15bc5a98
								
							
						
					
					
						commit
						492efbcba3
					
				| @ -1,8 +1,11 @@ | ||||
| #include "genlib.h" | ||||
| #include <math.h> | ||||
| #include <stdio.h> | ||||
| #include "log.h" | ||||
| 
 | ||||
| /*exponential covariance function*/ | ||||
| double exponential(double h) { | ||||
|     log_info("RESULT = in progress, h = %f", h); | ||||
| 
 | ||||
|     return (exp(-3. * (double)h)); | ||||
| } | ||||
| @ -1,15 +1,50 @@ | ||||
| #include "genlib.h" | ||||
| #include "log.h" | ||||
| #include "memory.h" | ||||
| #include <math.h> | ||||
| #include <stdio.h> | ||||
| #include <time.h> | ||||
| 
 | ||||
| /*gamma covariance function*/ | ||||
| double gammf(double h, double alpha) { | ||||
| double gammf(double h, double alpha, int cores) { | ||||
|     double* used_ram_t0 = malloc(sizeof(double)); | ||||
|     getVirtualMemUsed(used_ram_t0); | ||||
| 
 | ||||
|     clock_t t = clock(); | ||||
| 
 | ||||
|     log_info("RESULT = in progress, h = %f, alpha = %f", h, alpha); | ||||
| 
 | ||||
|     struct cpustat initial[cores]; | ||||
|     struct cpustat final[cores]; | ||||
| 
 | ||||
|     for (int i = 0; i < cores; i++) { | ||||
|         get_stats(&initial[i], i - 1); | ||||
|     } | ||||
| 
 | ||||
|     float delta; | ||||
|     double z; | ||||
| 
 | ||||
|     delta = pow(20., 1. / alpha) - 1.; | ||||
|     z = 1. / (double)(pow(1. + h * delta, alpha)); | ||||
| 
 | ||||
|     t = clock() - t; | ||||
|     double time_taken = ((double)t)/CLOCKS_PER_SEC; // calculate the elapsed time
 | ||||
| 
 | ||||
|     for (int i = 0; i < cores; i++) { | ||||
|         get_stats(&final[i], i - 1); | ||||
|     } | ||||
| 
 | ||||
|     for (int i = 0; i < cores; i++) { | ||||
|         log_info("CPU %d: %lf%%", i, calculate_load(&initial[i], &final[i])); | ||||
|     } | ||||
| 
 | ||||
|     double* used_ram_tf = malloc(sizeof(double)); | ||||
|     getVirtualMemUsed(used_ram_t0); | ||||
| 
 | ||||
|     log_info("RESULT = success, delta = %f, z = %f, ELAPSED = %f seconds, DIF USED VIRTUAL MEM = %5.1f MB", delta, z, time_taken, *used_ram_tf - *used_ram_t0); | ||||
| 
 | ||||
|     free(used_ram_t0); | ||||
|     free(used_ram_tf); | ||||
| 
 | ||||
|     return z; | ||||
| } | ||||
| @ -1,8 +1,10 @@ | ||||
| #include "genlib.h" | ||||
| #include "log.h" | ||||
| #include <math.h> | ||||
| #include <stdio.h> | ||||
| 
 | ||||
| /*gaussian covariance function*/ | ||||
| double gaussian(double h) { | ||||
|     log_info("RESULT = in progress, h = %f", h); | ||||
|     return (exp(-3. * (double)(h * h))); | ||||
| } | ||||
| @ -0,0 +1,177 @@ | ||||
| /*
 | ||||
|  * Copyright (c) 2020 rxi | ||||
|  * | ||||
|  * Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
|  * of this software and associated documentation files (the "Software"), to | ||||
|  * deal in the Software without restriction, including without limitation the | ||||
|  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or | ||||
|  * sell copies of the Software, and to permit persons to whom the Software is | ||||
|  * furnished to do so, subject to the following conditions: | ||||
|  * | ||||
|  * The above copyright notice and this permission notice shall be included in | ||||
|  * all copies or substantial portions of the Software. | ||||
|  * | ||||
|  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||
|  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||
|  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||
|  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||
|  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||||
|  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | ||||
|  * IN THE SOFTWARE. | ||||
|  */ | ||||
| 
 | ||||
| #include <stdlib.h> | ||||
| #include "log.h" | ||||
| #include <string.h> | ||||
| 
 | ||||
| #define MAX_CALLBACKS 32 | ||||
| 
 | ||||
| typedef struct { | ||||
|   log_LogFn fn; | ||||
|   void *udata; | ||||
|   int level; | ||||
| } Callback; | ||||
| 
 | ||||
| static struct { | ||||
|   void *udata; | ||||
|   log_LockFn lock; | ||||
|   int level; | ||||
|   bool quiet; | ||||
|   Callback callbacks[MAX_CALLBACKS]; | ||||
| } L; | ||||
| 
 | ||||
| 
 | ||||
| static const char *level_strings[] = { | ||||
|   "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL" | ||||
| }; | ||||
| 
 | ||||
| #ifdef LOG_USE_COLOR | ||||
| static const char *level_colors[] = { | ||||
|   "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m" | ||||
| }; | ||||
| #endif | ||||
| 
 | ||||
| 
 | ||||
| static void stdout_callback(log_Event *ev) { | ||||
|   char buf[16]; | ||||
|   buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0'; | ||||
| #ifdef LOG_USE_COLOR | ||||
|   fprintf( | ||||
|     ev->udata, "%s %s%-5s\x1b[0m \x1b[0m%s:%d:\x1b[0m ", | ||||
|     buf, level_colors[ev->level], level_strings[ev->level], | ||||
|     ev->file, ev->line); | ||||
| #else | ||||
|   fprintf( | ||||
|     ev->udata, "%s %-5s %s:%d: ", | ||||
|     buf, level_strings[ev->level], ev->file, ev->line); | ||||
| #endif | ||||
|   vfprintf(ev->udata, ev->fmt, ev->ap); | ||||
|   fprintf(ev->udata, "\n"); | ||||
|   fflush(ev->udata); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| static void file_callback(log_Event *ev) { | ||||
|   char buf[64]; | ||||
|   buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0'; | ||||
|   fprintf( | ||||
|     ev->udata, "%s %-5s %s:%d: ", | ||||
|     buf, level_strings[ev->level], ev->file, ev->line); | ||||
|   vfprintf(ev->udata, ev->fmt, ev->ap); | ||||
|   fprintf(ev->udata, "\n"); | ||||
|   fflush(ev->udata); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| static void lock(void)   { | ||||
|   if (L.lock) { L.lock(true, L.udata); } | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| static void unlock(void) { | ||||
|   if (L.lock) { L.lock(false, L.udata); } | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| const char* log_level_string(int level) { | ||||
|   return level_strings[level]; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void log_set_lock(log_LockFn fn, void *udata) { | ||||
|   L.lock = fn; | ||||
|   L.udata = udata; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void log_set_level(int level) { | ||||
|   L.level = level; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void log_set_quiet(bool enable) { | ||||
|   L.quiet = enable; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| int log_add_callback(log_LogFn fn, void *udata, int level) { | ||||
|   for (int i = 0; i < MAX_CALLBACKS; i++) { | ||||
|     if (!L.callbacks[i].fn) { | ||||
|       L.callbacks[i] = (Callback) { fn, udata, level }; | ||||
|       return 0; | ||||
|     } | ||||
|   } | ||||
|   return -1; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| int log_add_fp(FILE *fp, int level) { | ||||
|   return log_add_callback(file_callback, fp, level); | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| static void init_event(log_Event *ev, void *udata) { | ||||
|   if (!ev->time) { | ||||
|     time_t t = time(NULL); | ||||
|     ev->time = localtime(&t); | ||||
|   } | ||||
|   ev->udata = udata; | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| void log_log(int level, const char *file, int line, const char *fmt, ...) { | ||||
|   log_Event ev = { | ||||
|     .fmt   = fmt, | ||||
|     .file  = file, | ||||
|     .line  = line, | ||||
|     .level = level, | ||||
|   }; | ||||
| 
 | ||||
|   char* env_var = getenv("ENV"); | ||||
|   if (env_var != NULL && strcmp("false", env_var) == 0) return; | ||||
|    | ||||
|   char* substr_mem = strstr(fmt, "MEM"); | ||||
|   char* substr_cpu = strstr(fmt, "CPU"); | ||||
|   if (env_var != NULL && strcmp("analysis", env_var) == 0 && substr_mem == NULL && substr_cpu == NULL) return; | ||||
| 
 | ||||
|   lock(); | ||||
| 
 | ||||
|   if (!L.quiet && level >= L.level) { | ||||
|     init_event(&ev, stderr); | ||||
|     va_start(ev.ap, fmt); | ||||
|     stdout_callback(&ev); | ||||
|     va_end(ev.ap); | ||||
|   } | ||||
| 
 | ||||
|   for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) { | ||||
|     Callback *cb = &L.callbacks[i]; | ||||
|     if (level >= cb->level) { | ||||
|       init_event(&ev, cb->udata); | ||||
|       va_start(ev.ap, fmt); | ||||
|       cb->fn(&ev); | ||||
|       va_end(ev.ap); | ||||
|     } | ||||
|   } | ||||
| 
 | ||||
|   unlock(); | ||||
| } | ||||
| @ -0,0 +1,49 @@ | ||||
| /**
 | ||||
|  * Copyright (c) 2020 rxi | ||||
|  * | ||||
|  * This library is free software; you can redistribute it and/or modify it | ||||
|  * under the terms of the MIT license. See `log.c` for details. | ||||
|  */ | ||||
| 
 | ||||
| #ifndef LOG_H | ||||
| #define LOG_H | ||||
| 
 | ||||
| #include <stdio.h> | ||||
| #include <stdarg.h> | ||||
| #include <stdbool.h> | ||||
| #include <time.h> | ||||
| 
 | ||||
| #define LOG_VERSION "0.1.0" | ||||
| 
 | ||||
| typedef struct { | ||||
|   va_list ap; | ||||
|   const char *fmt; | ||||
|   const char *file; | ||||
|   struct tm *time; | ||||
|   void *udata; | ||||
|   int line; | ||||
|   int level; | ||||
| } log_Event; | ||||
| 
 | ||||
| typedef void (*log_LogFn)(log_Event *ev); | ||||
| typedef void (*log_LockFn)(bool lock, void *udata); | ||||
| 
 | ||||
| enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL }; | ||||
| 
 | ||||
| #define log_trace(...) log_log(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__) | ||||
| #define log_debug(...) log_log(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__) | ||||
| #define log_info(...)  log_log(LOG_INFO,  __FILE__, __LINE__, __VA_ARGS__) | ||||
| #define log_warn(...)  log_log(LOG_WARN,  __FILE__, __LINE__, __VA_ARGS__) | ||||
| #define log_error(...) log_log(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__) | ||||
| #define log_fatal(...) log_log(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__) | ||||
| 
 | ||||
| const char* log_level_string(int level); | ||||
| void log_set_lock(log_LockFn fn, void *udata); | ||||
| void log_set_level(int level); | ||||
| void log_set_quiet(bool enable); | ||||
| int log_add_callback(log_LogFn fn, void *udata, int level); | ||||
| int log_add_fp(FILE *fp, int level); | ||||
| 
 | ||||
| void log_log(int level, const char *file, int line, const char *fmt, ...); | ||||
| 
 | ||||
| #endif | ||||
| @ -0,0 +1,94 @@ | ||||
| #include <sys/types.h> | ||||
| #include <sys/sysinfo.h> | ||||
| #include <stdlib.h> | ||||
| #include <stdio.h> | ||||
| #include <string.h> | ||||
| #include <sys/times.h> | ||||
| #include <sys/vtimes.h> | ||||
| #include <unistd.h> | ||||
| #include "log.h" | ||||
| #include "memory.h" | ||||
| 
 | ||||
| static unsigned long long lastTotalUser, lastTotalUserLow, lastTotalSys, lastTotalIdle; | ||||
| static clock_t lastCPU, lastSysCPU, lastUserCPU; | ||||
| static int numProcessors; | ||||
| 
 | ||||
| void getTotalVirtualMem(double* total_ram) { | ||||
|     const double megabyte = 1024 * 1024; | ||||
|     struct sysinfo si; | ||||
|     sysinfo(&si); | ||||
|     *total_ram = si.totalram / megabyte; | ||||
| } | ||||
| 
 | ||||
| void getVirtualMemUsed(double* used_ram) { | ||||
|     const double megabyte = 1024 * 1024; | ||||
|     struct sysinfo si; | ||||
|     sysinfo(&si); | ||||
|     *used_ram = (si.totalram - si.freeram) / megabyte; | ||||
| } | ||||
| 
 | ||||
| int parseLine(char* line) { | ||||
|     // This assumes that a digit will be found and the line ends in " Kb".
 | ||||
|     int i = strlen(line); | ||||
|     const char* p = line; | ||||
|     while (*p <'0' || *p > '9') p++; | ||||
|     line[i-3] = '\0'; | ||||
|     i = atoi(p); | ||||
|     return i; | ||||
| } | ||||
| 
 | ||||
| int getVirtualMemUsedByCurrentProcess() {  | ||||
|     FILE* file = fopen("/proc/self/status", "r"); | ||||
|     int result = -1; | ||||
|     char line[128]; | ||||
| 
 | ||||
|     while (fgets(line, 128, file) != NULL){ | ||||
|         if (strncmp(line, "VmSize:", 7) == 0){ | ||||
|             result = parseLine(line); | ||||
|             break; | ||||
|         } | ||||
|     } | ||||
|     fclose(file); | ||||
|     return result / 1024; | ||||
| } | ||||
| 
 | ||||
| void skip_lines(FILE *fp, int numlines) {  | ||||
|     int cnt = 0; | ||||
|     char ch; | ||||
|     while ((cnt < numlines) && ((ch = getc(fp)) != EOF)) { | ||||
|         if (ch == '\n') cnt++; | ||||
|     } | ||||
| } | ||||
| 
 | ||||
| void get_stats(struct cpustat *st, int cpunum) { | ||||
|     FILE *fp = fopen("/proc/stat", "r"); | ||||
|     int lskip = cpunum+1; | ||||
|     skip_lines(fp, lskip); | ||||
|     char cpun[255]; | ||||
|     fscanf(fp, "%s %d %d %d %d %d %d %d", cpun, &(st->t_user), &(st->t_nice),  | ||||
|         &(st->t_system), &(st->t_idle), &(st->t_iowait), &(st->t_irq), | ||||
|         &(st->t_softirq)); | ||||
|     fclose(fp); | ||||
| 	return; | ||||
| } | ||||
| 
 | ||||
| double calculate_load(struct cpustat *prev, struct cpustat *cur) { | ||||
|     int idle_prev = (prev->t_idle) + (prev->t_iowait); | ||||
|     int idle_cur = (cur->t_idle) + (cur->t_iowait); | ||||
| 
 | ||||
|     int nidle_prev = (prev->t_user) + (prev->t_nice) + (prev->t_system) + (prev->t_irq) + (prev->t_softirq); | ||||
|     int nidle_cur = (cur->t_user) + (cur->t_nice) + (cur->t_system) + (cur->t_irq) + (cur->t_softirq); | ||||
| 
 | ||||
|     int total_prev = idle_prev + nidle_prev; | ||||
|     int total_cur = idle_cur + nidle_cur; | ||||
| 
 | ||||
|     double totald = (double) total_cur - (double) total_prev; | ||||
| 
 | ||||
|     double idled = (double) idle_cur - (double) idle_prev; | ||||
| 
 | ||||
|     if (totald == 0 && idled == 0) return 0;  | ||||
| 
 | ||||
|     double cpu_perc = (1000 * (totald - idled) / totald + 1) / 10; | ||||
| 
 | ||||
|     return cpu_perc; | ||||
| } | ||||
| @ -0,0 +1,24 @@ | ||||
| #include "sys/types.h" | ||||
| #include "sys/sysinfo.h" | ||||
| #include "stdlib.h" | ||||
| #include "stdio.h" | ||||
| #include "string.h" | ||||
| #include "sys/times.h" | ||||
| #include "sys/vtimes.h" | ||||
| 
 | ||||
| void getTotalVirtualMem(); | ||||
| void getVirtualMemUsed(); | ||||
| int getVirtualMemUsedByCurrentProcess(); | ||||
| 
 | ||||
| struct cpustat { | ||||
|     unsigned long t_user; | ||||
|     unsigned long t_nice; | ||||
|     unsigned long t_system; | ||||
|     unsigned long t_idle; | ||||
|     unsigned long t_iowait; | ||||
|     unsigned long t_irq; | ||||
|     unsigned long t_softirq; | ||||
| }; | ||||
| 
 | ||||
| void get_stats(struct cpustat *st, int cpunum); | ||||
| double calculate_load(struct cpustat *prev, struct cpustat *cur); | ||||
| @ -1,8 +1,10 @@ | ||||
| #include "genlib.h" | ||||
| #include <math.h> | ||||
| #include <stdio.h> | ||||
| #include "log.h" | ||||
| 
 | ||||
| /*power covariance function*/ | ||||
| double power(double h, double alpha) { | ||||
|     log_info("RESULT = in progress, h = %f, alpha = %f", h, alpha); | ||||
|     return pow(h, alpha); | ||||
| } | ||||
|  | ||||
| @ -0,0 +1,2 @@ | ||||
| python3 setup.py install --user | ||||
| ENV=analysis python3 example.py $1 2>&1 | split -l 5000000 - log_$1_improvement-chunk-array-   | ||||
					Loading…
					
					
				
		Reference in New Issue