File: mm.c

package info (click to toggle)
massivethreads 1.02-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,924 kB
  • sloc: ansic: 27,814; sh: 4,559; cpp: 3,334; javascript: 1,799; makefile: 1,745; python: 523; asm: 373; perl: 118; lisp: 9
file content (252 lines) | stat: -rw-r--r-- 5,691 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
 * mm.c
 * Memory management/migration 
 *
 * by Nan Dun <dun@logos.ic.i.u-tokyo.ac.jp>
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <pthread.h>
#include <sys/time.h>

#define ARR_SIZE  1
#define BLK_SIZE  1048576

#define MEM_TRACE

#ifdef MEM_TRACE
#include <mcheck.h>
#endif

struct data {
  char **arr;
  int mem_allocated;
  double t_alloc, t_free, t_realloc, t_refree, t_total;
};

inline double curr_time(void)
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec * 1e6 + (double) tv.tv_usec;
}

void * malloc_local(void *args)
{
  struct data *p = (struct data *) args;
  unsigned long tid;
  int i;
  
  tid = pthread_self();
  
  // malloc
  printf("  Thread %lu: malloc\n", tid);
  p->t_alloc = curr_time();
  p->arr = malloc(sizeof(char *) * ARR_SIZE);
  if (p->arr == NULL) {
    perror(NULL);
    abort();
  }
  for (i = 0; i < ARR_SIZE; i++) {
    p->arr[i] = (char *) malloc(sizeof(char) * BLK_SIZE);
    if (p->arr[i] == NULL) {
      perror(NULL);
      abort();
    }
  }
  p->t_alloc = curr_time() - p->t_alloc;

  // free
  printf("  Thread %lu: free\n", tid);
  p->t_free = curr_time();
  for (i = 0; i < ARR_SIZE; i++) 
    free(p->arr[i]);
  free(p->arr);
  p->t_free = curr_time() - p->t_free;

  // re-malloc
  printf("  Thread %lu: re-malloc\n", tid);
  p->t_realloc = curr_time();
  p->arr = malloc(sizeof(char *) * ARR_SIZE);
  if (p->arr == NULL) {
    perror(NULL);
    abort();
  }
  for (i = 0; i < ARR_SIZE; i++) {
    p->arr[i] = (char *) malloc(sizeof(char) * BLK_SIZE);
    if (p->arr[i] == NULL) {
      perror(NULL);
      abort();
    }
  }
  p->t_realloc = curr_time() - p->t_realloc;
  
  // re-free
  printf("  Thread %lu: re-free\n", tid);
  p->t_refree = curr_time();
  for (i = 0; i < ARR_SIZE; i++)
    free(p->arr[i]);
  free(p->arr);
  p->t_refree = curr_time() - p->t_refree;

  p->t_total = p->t_alloc + p->t_free + p->t_realloc + p->t_refree;

  printf("  Thread %lu: exit\n", tid);
  pthread_exit((void *) 0);
}

pthread_mutex_t mutex;
pthread_cond_t cond;

void * malloc_remote_from(void *args)
{
  struct data *p = (void *) args;
  unsigned long tid;
  int i;
 
  tid = pthread_self();
  
  pthread_mutex_lock(&mutex);

  // malloc
  printf("  Thread %lu: malloc\n", tid);
  p->t_alloc = curr_time();
  p->arr = (char **) malloc(sizeof(char *) * ARR_SIZE);
  if (p->arr == NULL) {
    perror(NULL);
    abort();
  }
  for (i = 0; i < ARR_SIZE; i++) {
    (p->arr)[i] = (char *) malloc(sizeof(char) * BLK_SIZE);
    if ((p->arr)[i] == NULL) {
      perror(NULL);
      abort();
    }
  }
  p->t_alloc = curr_time() - p->t_alloc;

  p->mem_allocated = 1;
  pthread_cond_signal(&cond);
  pthread_mutex_unlock(&mutex);

  pthread_mutex_lock(&mutex);
  while (p->mem_allocated == 1)
    pthread_cond_wait(&cond, &mutex);
  pthread_mutex_unlock(&mutex);
  
  p->t_total = p->t_alloc + p->t_free + p->t_realloc + p->t_refree;

  printf("  Thread %lu: exit\n", tid);
  pthread_exit((void *) 0);
}

void * malloc_remote_to(void *args)
{
  struct data *p = (void *) args;
  unsigned long tid;
  int i;
  
  tid = pthread_self();
  pthread_mutex_lock(&mutex);
  while (p->mem_allocated == 0)
    pthread_cond_wait(&cond, &mutex);
  pthread_mutex_unlock(&mutex);

  // free
  printf("  Thread %lu: free\n", tid);
  p->t_free = curr_time();
  for (i = 0; i < ARR_SIZE; i++) 
    free(p->arr[i]);
  free(p->arr);
  p->t_free = curr_time() - p->t_free;
  
  // re-malloc
  printf("  Thread %lu: re-malloc\n", tid);
  p->t_realloc = curr_time();
  p->arr = malloc(sizeof(char *) * ARR_SIZE);
  if (p->arr == NULL) {
    perror(NULL);
    abort();
  }
  for (i = 0; i < ARR_SIZE; i++) {
    p->arr[i] = (char *) malloc(sizeof(char) * BLK_SIZE);
    if (p->arr[i] == NULL) {
      perror(NULL);
      abort();
    }
  }
  p->t_realloc = curr_time() - p->t_realloc;

  // re-free
  printf("  Thread %lu: re-free\n", tid);
  p->t_refree = curr_time();
  for (i = 0; i < ARR_SIZE; i++)
    free(p->arr[i]);
  free(p->arr);
  p->t_refree = curr_time() - p->t_refree;

#ifdef MEM_TRACE
  muntrace();
#endif
  
  pthread_mutex_lock(&mutex);
  p->mem_allocated = 0;
  pthread_cond_signal(&cond);
  pthread_mutex_unlock(&mutex);
  
  printf("  Thread %lu: exit\n", tid);
  pthread_exit((void *) 0);
}

int main(int argc, char **argv)
{
  pthread_t tha, thb;
  struct data dat;
  int nthreads;
  int ret;

  nthreads = argc == 1 ? 1 : atoi(argv[1]);
  
  printf("Memory management/migration test (usec)\n");

#ifdef MEM_TRACE
  mtrace();
#endif
  
  if (nthreads == 1) {
      printf("One thread...\n");
      pthread_create(&tha, NULL, malloc_local, (void *) &dat);
      pthread_join(tha, (void **) &ret);
      
      printf("     malloc: %.2f\n"
             "       free: %.2f\n"
             "  re-malloc: %.2f\n"
             "    re-free: %.2f\n"
             "      Total: %.2f\n", dat.t_alloc, dat.t_free, 
             dat.t_realloc, dat.t_refree, dat.t_total);
  }

  if (nthreads >= 2) {
      printf("Two threads...\n");
      dat.mem_allocated = 0;
      pthread_mutex_init(&mutex, NULL);
      pthread_cond_init(&cond, NULL);
      pthread_create(&tha, NULL, malloc_remote_from, (void *) &dat);
      pthread_create(&thb, NULL, malloc_remote_to, (void *) &dat);
      pthread_join(tha, (void **) &ret);
      pthread_join(thb, (void **) &ret);
      pthread_mutex_destroy(&mutex);
      pthread_cond_destroy(&cond);
      
      printf("     malloc: %.2f\n"
             "       free: %.2f\n"
             "  re-malloc: %.2f\n"
             "    re-free: %.2f\n"
             "      Total: %.2f\n", dat.t_alloc, dat.t_free, 
             dat.t_realloc, dat.t_refree, dat.t_total);
  }

  return 0;
}