forked from OCamlPro/gnucobol
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6b44051
commit 4250dd7
Showing
5 changed files
with
235 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/* | ||
Copyright (C) 2003-2023 Free Software Foundation, Inc. | ||
Written by Emilien Lemaire | ||
This file is part of GnuCOBOL. | ||
The GnuCOBOL compiler 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. | ||
GnuCOBOL 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 GnuCOBOL. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#include "cobperf.h" | ||
|
||
#include <stddef.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
#include <time.h> | ||
|
||
struct linedata { | ||
const char *filename; | ||
unsigned line_number; | ||
const char *procedure_name; | ||
const char *paragraph_name; | ||
}; | ||
|
||
struct rundata { | ||
long long total_time; | ||
unsigned count; | ||
linedata line_data; | ||
}; | ||
|
||
|
||
typedef struct array { | ||
size_t capacity; | ||
size_t size; | ||
void **data; | ||
} array ; | ||
|
||
typedef void (*array_callback)(void *data, size_t idx, void *usr); | ||
|
||
#define ARRAY_DEFAULT_CAPACITY 128 | ||
#define ARRAY_GROW_FACTOR 2 | ||
|
||
static array *array_init(size_t capacity) { | ||
array *arr = (array*)malloc(sizeof(array)); | ||
|
||
if (arr == NULL) return arr; | ||
|
||
arr->capacity = capacity; | ||
arr->size = 0; | ||
arr->data = calloc(capacity, sizeof(void*)); | ||
|
||
return arr; | ||
} | ||
|
||
static array *array_init_default() { | ||
return array_init(ARRAY_DEFAULT_CAPACITY); | ||
} | ||
|
||
static void array_resize(array *arr) { | ||
void **old_data = arr->data; | ||
arr->capacity *= ARRAY_GROW_FACTOR; | ||
arr->data = (void**)calloc(arr->capacity, sizeof(void*)); | ||
for (size_t i = 0; i < arr->size; i++) { | ||
arr->data[i] = old_data[i]; | ||
} | ||
free(old_data); | ||
} | ||
|
||
static void array_push(array* arr, void *data) { | ||
arr->data[arr->size] = data; | ||
arr->size++; | ||
if (arr->size == arr->capacity) { | ||
array_resize(arr); | ||
} | ||
} | ||
|
||
static void array_iter(array *arr, array_callback cb, void *usr) { | ||
for (size_t i = 0; i < arr->size; ++i) { | ||
cb(arr->data[i], i, usr); | ||
} | ||
} | ||
|
||
static void array_free(array *arr) { | ||
free(arr->data); | ||
free(arr); | ||
} | ||
|
||
static array *arr; | ||
struct timespec timestamp; | ||
static linedata *data = NULL; | ||
|
||
void cobperf_init() { | ||
arr = array_init_default(); | ||
clock_gettime(CLOCK_MONOTONIC, ×tamp); | ||
} | ||
|
||
void cobperf_runline( | ||
const char *filename, | ||
unsigned line_number, | ||
const char *procedure_name, | ||
const char *paragraph_name) | ||
{ | ||
struct timespec lasttime = timestamp; | ||
clock_gettime(CLOCK_MONOTONIC, ×tamp); | ||
long long timediff = timestamp.tv_nsec - lasttime.tv_nsec; | ||
|
||
if (data == NULL) { | ||
data = (linedata*)malloc(sizeof(linedata)); | ||
} else { | ||
rundata *run = (rundata*)malloc(sizeof(rundata)); | ||
run->count = 1; | ||
run->total_time = timediff; | ||
run->line_data = *data; | ||
array_push(arr, (void *)run); | ||
} | ||
data->filename = filename; | ||
data->line_number = line_number; | ||
data->procedure_name = procedure_name; | ||
data->paragraph_name = paragraph_name; | ||
} | ||
|
||
static void run_data_free(void *data, size_t idx, void *_) { | ||
rundata *rdata = (rundata*)data; | ||
free(rdata); | ||
} | ||
|
||
static void cobperf_free() { | ||
array_iter(arr, run_data_free, NULL); | ||
array_free(arr); | ||
} | ||
|
||
/*todo: aggregate data*/ | ||
static void write_rundata_csv(void *data, size_t _, void *file) { | ||
FILE *f = (FILE*)file; | ||
rundata *rdata = (rundata*)data; | ||
|
||
fprintf(f, "%s, %s, %s, %u ,%u, %lld\n", | ||
rdata->line_data.filename, | ||
rdata->line_data.procedure_name, | ||
rdata->line_data.paragraph_name, | ||
rdata->line_data.line_number, | ||
rdata->count, | ||
rdata->total_time); | ||
} | ||
|
||
void cobperf_end() { | ||
struct timespec lasttime = timestamp; | ||
clock_gettime(CLOCK_MONOTONIC, ×tamp); | ||
long long timediff = timestamp.tv_nsec - lasttime.tv_nsec; | ||
|
||
rundata *rdata = (rundata*)malloc(sizeof(rundata)); | ||
rdata->count = 1; | ||
rdata->total_time = timediff; | ||
rdata->line_data = *data; | ||
array_push(arr, (void *)rdata); | ||
|
||
FILE *f = fopen("prof.csv", "w"); | ||
fprintf(f, "Filename, Section, Paragraph, Line, Count, Time\n"); | ||
array_iter(arr, write_rundata_csv, (void*)f); | ||
fclose(f); | ||
|
||
cobperf_free(); | ||
free(data); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright (C) 2003-2023 Free Software Foundation, Inc. | ||
Written by Emilien Lemaire | ||
This file is part of GnuCOBOL. | ||
The GnuCOBOL compiler 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. | ||
GnuCOBOL 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 GnuCOBOL. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#ifndef cobperf_h | ||
#define cobperf_h | ||
|
||
typedef struct linedata linedata; | ||
|
||
typedef struct rundata rundata; | ||
|
||
void cobperf_init(void); | ||
|
||
/** | ||
* Logs a line run. | ||
* @param filename the COBOL filename | ||
* @param line_number the number of the line that runs | ||
* @param section the section name | ||
* @param paragraph the paragraph name | ||
*/ | ||
void cobperf_runline(const char *filename, unsigned line_number, const char* section, const char* paragraph); | ||
|
||
/** | ||
* This function will save all the logs to the log file. | ||
*/ | ||
void cobperf_end(void); | ||
|
||
#endif | ||
|