-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmembench.c
More file actions
98 lines (77 loc) · 1.86 KB
/
membench.c
File metadata and controls
98 lines (77 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include "header.h"
typedef struct
{
int allocations; /* Number of allocations */
int size; /* Size of allocation in bytes */
} memory_parameters;
void Usage()
{
printf("ERROR\n");
printf("USAGE ./membench <Threads> <Allocations> <Size>\n");
}
void* allocate_memory(int size)
{
return malloc(size);
}
void deallocate_memory(void* memory)
{
free(memory);
}
void* threads_exe(void* arg)
{
memory_parameters memp = *(memory_parameters*)arg;
//void* temp_memory = allocate_memory(memp.size);
for(int i = 0; i < memp.allocations; i++)
{
//printf("(%d %d)", i, memp.size);
void* temp_memory = allocate_memory(memp.size);
pthread_cleanup_push(deallocate_memory, temp_memory);
pthread_cleanup_pop(1);
}
/* Register a cleanup handler for this buffer, to deallocate it in case the thread exits or is cancelled. */
//pthread_cleanup_push(deallocate_memory, temp_memory);
/* Exit current Thread */
//pthread_t tid = pthread_self();
//pthread_exit(&tid);
/* Unregister the cleanup handler. Because we pass a nonzero value,this actually performs the cleanup by callingdeallocate_buffer. */
//pthread_cleanup_pop(1);
return NULL;
}
int main(int argc, char* argv[])
{
if(argc < 4)
{
Usage();
exit(EXIT_FAILURE);
}
int array[3];
for(int i = 0; i < argc - 1; i++)
{
if (sscanf (argv[i + 1], "%i", &array[i]) != 1)
{
fprintf(stderr, "error - not an integer");
}
}
pthread_t tid[array[0]];
memory_parameters mp;
mp.allocations = array[1];
mp.size = array[2];
for(int i = 0; i < array[0]; i++)
{
if(pthread_create(&tid[i], NULL, threads_exe, &mp))
{
perror("pthread_create");
exit(EXIT_FAILURE);
}
}
for(int i = 0; i < array[0]; i++)
{
if(pthread_join(tid[i], NULL))
{
perror("pthread_join");
exit(EXIT_FAILURE);
}
}
return EXIT_SUCCESS;
}
// valgrind --leak-check=yes ./m 10 10 10