Skip to content

Commit 1d88ae2

Browse files
committed
Review fixes
1 parent 0f6c5c7 commit 1d88ae2

File tree

8 files changed

+54
-63
lines changed

8 files changed

+54
-63
lines changed

lib/profiler/README.md

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,52 @@
1-
# Benchmark
1+
# Profiler
22

33
A simple library to benchmark your code.
44

5-
## Compiling
6-
7-
- `mkdir build`
8-
- `cd build`
9-
- `cmake ..`
10-
- `make`
11-
125
## Usage example
136

147
```c
158
#include <stdio.h>
16-
#include "benchmark.h"
9+
#define PROFILER
10+
#include "profiler.h"
1711

18-
int main(int argc, char **argv)
12+
static void random_func(void)
1913
{
20-
(void) argc;
21-
(void) argv;
22-
23-
/* Create an initate the stopclock */
24-
StopClock sclock;
25-
sclock_init(&sclock);
26-
27-
sclock_start(&sclock);
28-
29-
/* Execute some code */
30-
31-
sclock_checkpoint(&sclock, "First part");
14+
TIME_FUNC_BEGIN();
3215

33-
/* Execute some more code */
16+
/* Your code goes here... */
3417

35-
sclock_checkpoint(&sclock, "Second part");
36-
37-
/* Execute even more code */
18+
TIME_FUNC_END();
19+
}
3820

39-
sclock_checkpoint(&sclock, "Third part");
21+
int main(int argc, char **argv)
22+
{
23+
PROFILER_SETUP();
24+
(void) argc;
25+
(void) argv;
4026

41-
/* Stop the clock */
42-
sclock_stop(&sclock);
27+
random_func();
4328

44-
/* Output the results */
45-
sclock_print(&sclock, stdout);
29+
TIME_BLOCK_BEGIN(block_name);
30+
/* Your code goes here */
31+
TIME_BLOCK_END(block_name);
4632

47-
/* Clean up */
48-
sclock_destroy(&sclock);
33+
PROFILER_STOP(stdout);
4934
}
5035
```
5136
5237
### Example output
5338
54-
```
55-
===== Benchmarks : =====
56-
First part : 5916119358 95.54%
57-
Second part : 6940092 0.17%
58-
Third part : 265775256 4.29%
59-
Remaining time : 3478810 0.00%
39+
```text
40+
===== Benchmarks : =====
41+
Total time : 106691427524 (34.7301 s)
42+
run_game_update[1933] : 20781332 0.02% (2.53% w/children)
43+
update_before_particle_engine[1933] : 195193852 0.18%
44+
update_after_particle_engine[1933] : 1123800376 1.05%
45+
particle_engine_update[1933] : 1354889225 1.27%
6046
```
6147

6248
## How it works
6349

6450
Under the hood `benchmark` is using the `RDTSC` assembly instruction to time
65-
things. During the `sclock_init` the number of pseudo clocks used in `RDTSC`
66-
occur in one second (over 300ms). This is a rough estimate but usually aligns
67-
well with the Ghz number specified on the running computers cpu.
68-
69-
## API
70-
71-
> TODO: Generate API and link to it here
51+
things. Then there's a bunch of other logic that I don't remember because it's
52+
been a while since I wrote it.

lib/profiler/include/macros.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
#define PROFILER_STOP(fp)
3737
#define TIME_BLOCK_BEGIN(label)
3838
#define TIME_BLOCK_END(label)
39-
#define TIME_BANDWIDTH_BEGIN(label)
39+
#define TIME_BANDWIDTH_BEGIN(label, bytes)
4040
#define TIME_BANDWIDTH_END(label)
4141
#define TIME_FUNC_BEGIN()
4242
#define TIME_FUNC_END()

lib/profiler/include/repetition_tester.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
do { \
1010
fprintf(stderr, "%s()[%d] %s\n", __func__, __LINE__, msg); \
1111
tester->mode = TestMode_Error; \
12-
} while (0);
12+
} while (0)
1313

1414
typedef enum TestMode {
1515
TestMode_Uninitialized,

lib/profiler/src/calc_cpu_freq.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int main(void)
2121
u64 cpu_elapsed = cpu_end - cpu_start;
2222
u64 cpu_freq = 0;
2323
if (os_elapsed) {
24-
cpu_freq = os_freq * cpu_elapsed / os_elapsed;
24+
cpu_freq = (u64)((double) os_freq * cpu_elapsed / os_elapsed);
2525
}
2626

2727
printf(" OS Freq: %lu (reported)\n", os_freq);

lib/profiler/src/perf.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
#include "perf.h"
1010

11-
static int _FD;
11+
static int _FD = 0;
1212

1313
static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
1414
int cpu, int group_fd, unsigned long flags)
@@ -24,6 +24,11 @@ void perf_setup(void)
2424
{
2525
static struct perf_event_attr pe;
2626

27+
if (_FD) {
28+
close(_FD);
29+
_FD = 0;
30+
}
31+
2732
pe.type = PERF_TYPE_SOFTWARE;
2833
pe.size = sizeof(pe);
2934
pe.config = PERF_COUNT_SW_PAGE_FAULTS;
@@ -55,7 +60,10 @@ uint64_t perf_read_page_fault_count(void)
5560
{
5661
size_t count = 0;
5762
if (_FD) {
58-
read(_FD, &count, sizeof(count));
63+
ssize_t bytes = read(_FD, &count, sizeof(count));
64+
if (bytes != sizeof(count)) {
65+
count = 0;
66+
}
5967
}
6068
return count;
6169
}

lib/profiler/src/rdtsc.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#define PROFILER 1
2-
31
#include <sys/time.h>
2+
#include <stdint.h>
43
#include "rdtsc.h"
54

65
#ifdef __arm__

lib/profiler/src/rep_return_data.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ static ReturnData variable_create_data(void)
4646
static ReturnData* malloc_create_data(void)
4747
{
4848
ReturnData *data = malloc(sizeof(ReturnData));
49+
if (!data) {
50+
return NULL;
51+
}
4952
data->fnum = 1.2;
5053
data->count = 100;
5154
data->dnum = 3.4;

src/main.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,16 +1410,6 @@ void close(void)
14101410
gWindow = NULL;
14111411
TTF_Quit();
14121412
SDL_Quit();
1413-
1414-
#ifdef PROFILER
1415-
FILE *fp = fopen("profile.txt", "w");
1416-
if (fp == NULL) {
1417-
PROFILER_STOP(stdout);
1418-
} else {
1419-
PROFILER_STOP(fp);
1420-
}
1421-
fclose(fp);
1422-
#endif
14231413
}
14241414

14251415
#ifdef CHECKSUM_VALIDATION
@@ -1489,5 +1479,15 @@ int main(int argc, char *argv[])
14891479
close();
14901480
PHYSFS_deinit();
14911481

1482+
#ifdef PROFILER
1483+
FILE *fp = fopen("profile.txt", "w");
1484+
if (fp == NULL) {
1485+
PROFILER_STOP(stdout);
1486+
} else {
1487+
PROFILER_STOP(fp);
1488+
fclose(fp);
1489+
}
1490+
#endif
1491+
14921492
return 0;
14931493
}

0 commit comments

Comments
 (0)