Debian Codegraph @

test/dapltest/mdep/linux/dapl_mdep_user.c

raw
1 /*
2 * Copyright (c) 2002-2005, Network Appliance, Inc. All rights reserved.
3 *
4 * This Software is licensed under one of the following licenses:
5 *
6 * 1) under the terms of the "Common Public License 1.0" a copy of which is
7 * in the file LICENSE.txt in the root directory. The license is also
8 * available from the Open Source Initiative, see
9 * http://www.opensource.org/licenses/cpl.php.
10 *
11 * 2) under the terms of the "The BSD License" a copy of which is in the file
12 * LICENSE2.txt in the root directory. The license is also available from
13 * the Open Source Initiative, see
14 * http://www.opensource.org/licenses/bsd-license.php.
15 *
16 * 3) under the terms of the "GNU General Public License (GPL) Version 2" a
17 * copy of which is in the file LICENSE3.txt in the root directory. The
18 * license is also available from the Open Source Initiative, see
19 * http://www.opensource.org/licenses/gpl-license.php.
20 *
21 * Licensee has the right to choose one of the above licenses.
22 *
23 * Redistributions of source code must retain the above copyright
24 * notice and one of the license notices.
25 *
26 * Redistributions in binary form must reproduce both the above copyright
27 * notice, one of the license notices in the documentation
28 * and/or other materials provided with the distribution.
29 */
30
31 #include "dapl_mdep.h"
32 #include "dapl_proto.h"
33
34 #include <pthread.h> /* needed for pthread_atfork() */
35 #include <sys/time.h>
36 #include <stdlib.h> /* needed for getenv() */
37 #include <signal.h> /* needed for thread setup */
38
39 #include <assert.h>
40 #include <errno.h>
41 #include <semaphore.h>
42 #include <stdint.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <stdarg.h> /* for printf */
47 #include <sys/time.h>
48 #include <syslog.h>
49 #include <netdb.h> /* for getaddrinfo */
50
51 /*
52 * Include files for setting up a network name
53 */
54 #include <unistd.h>
55 #include <sys/types.h>
56 #include <sys/socket.h>
57 #include <ctype.h>
58
59 static FILE *Stat_Fp = NULL;
60 # define DT_STAT_FILE "/proc/stat"
61
62 #include "dapl_test_data.h" /* for alloc_count */
63
64 /*
65 * Machine dependant initialization
66 */
67
68 void DT_Mdep_Init(void)
69 {
70 Stat_Fp = fopen(DT_STAT_FILE, "r");
71 if (NULL == Stat_Fp) {
72 perror("fopen of " DT_STAT_FILE " failed");
73 exit(1);
74 }
75 }
76
77 /*
78 * Machine dependant deinitialization
79 */
80
81 void DT_Mdep_End(void)
82 {
83 if (0 != fclose(Stat_Fp)) {
84 perror("fclose of " DT_STAT_FILE " failed");
85 exit(1);
86 }
87 }
88
89 /*
90 * Generate name of IB device
91 */
92
93 bool DT_Mdep_GetDefaultDeviceName(char *dapl_name)
94 {
95 strcpy(dapl_name, DT_MdepDeviceName);
96 return true;
97 }
98
99 /*
100 * Sleep specified number of milliseconds
101 */
102
103 void DT_Mdep_Sleep(int msec)
104 {
105 struct timespec t;
106 t.tv_sec = msec / 1000; /* Whole seconds */
107 t.tv_nsec = (msec % 1000) * 1000 * 1000;
108 nanosleep(&t, 0);
109 }
110
111 void DT_Mdep_Schedule(void)
112 {
113 /* nothing here */
114 }
115
116 /*
117 * Get system statistics including uptime and idle time
118 */
119
120 bool DT_Mdep_GetCpuStat(DT_CpuStat * cpu_stat)
121 {
122
123 #define DT_CPU_STAT_STR "cpu"
124 #define DT_CPU_STAT_BUFFER_SIZE 1024
125 #define DT_CPU_STAT_DELIMITER " "
126
127 static char buffer[DT_CPU_STAT_BUFFER_SIZE];
128
129 cpu_stat->user = 0;
130 cpu_stat->system = 0;
131 cpu_stat->idle = 0;
132
133 if (0 != fflush(Stat_Fp)) {
134 perror("fflush of " DT_STAT_FILE " failed");
135 exit(1);
136 }
137
138 for (;;) {
139 if (NULL == fgets(buffer, DT_CPU_STAT_BUFFER_SIZE, Stat_Fp)) {
140 printf(DT_CPU_STAT_STR " not found\n");
141 exit(1);
142 }
143
144 if (!strncmp(buffer, DT_CPU_STAT_STR, strlen(DT_CPU_STAT_STR))) {
145 break;
146 }
147 }
148
149 (void)strtok(buffer, DT_CPU_STAT_DELIMITER);
150 cpu_stat->user = strtoul(strtok(NULL, DT_CPU_STAT_DELIMITER), NULL, 0);
151 cpu_stat->user += strtoul(strtok(NULL, DT_CPU_STAT_DELIMITER), NULL, 0);
152 cpu_stat->system =
153 strtoul(strtok(NULL, DT_CPU_STAT_DELIMITER), NULL, 0);
154 cpu_stat->idle = strtoul(strtok(NULL, DT_CPU_STAT_DELIMITER), NULL, 0);
155
156 rewind(Stat_Fp);
157
158 return true;
159 }
160
161 /*
162 * Get current time in milliseconds (relative to some fixed point)
163 */
164 unsigned long DT_Mdep_GetTime(void)
165 {
166 struct timeval tv;
167 gettimeofday(&tv, NULL);
168 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
169 }
170
171 #ifdef RDTSC_TIMERS
172 double DT_Mdep_GetCpuMhz(void)
173 {
174 #define DT_TSC_BUFFER_SIZE 128
175 #if defined (__PPC__) || defined (__PPC64__)
176 #define DT_TSC_RATE "timebase"
177 #define DT_TSC_BASE 1000000.0
178 #else
179 #define DT_TSC_RATE "cpu MHz"
180 #define DT_TSC_BASE 1.0
181 #endif
182 #define DT_TSC_DELIMITER ":"
183
184 FILE *fp;
185 char buffer[DT_TSC_BUFFER_SIZE];
186 char *mhz_str;
187
188 fp = fopen("/proc/cpuinfo", "r");
189 if (NULL == fp) {
190 perror("fopen of /proc/cpuinfo failed");
191 exit(1);
192 }
193
194 for (;;) {
195 if (NULL == fgets(buffer, DT_TSC_BUFFER_SIZE, fp)) {
196 printf("cpu MHZ not found\n");
197 exit(1);
198 }
199
200 if (!strncmp(buffer, DT_TSC_RATE, strlen(DT_TSC_RATE))) {
201 (void)strtok(buffer, DT_TSC_DELIMITER);
202 mhz_str = strtok(NULL, DT_TSC_DELIMITER);
203
204 break;
205 }
206 }
207
208 if (0 != fclose(fp)) {
209 perror("fclose of /proc/cpuinfo failed");
210 exit(1);
211 }
212
213 return strtod(mhz_str, NULL) / DT_TSC_BASE;
214 }
215 #else /* !RDTSC_TIMERS */
216
217 double DT_Mdep_GetCpuMhz(void)
218 {
219 return 1;
220 }
221 #endif
222
223 unsigned long DT_Mdep_GetContextSwitchNum(void)
224 {
225
226 #define DT_CTXT_STR "ctxt"
227 #define DT_CTXT_BUFFER_SIZE 1024
228 #define DT_CTXT_DELIMITER " "
229
230 static char buffer[DT_CTXT_BUFFER_SIZE];
231 char *ctxt_str;
232
233 if (0 != fflush(Stat_Fp)) {
234 perror("fflush of " DT_STAT_FILE " failed");
235 exit(1);
236 }
237
238 for (;;) {
239 if (NULL == fgets(buffer, DT_CTXT_BUFFER_SIZE, Stat_Fp)) {
240 printf(DT_CTXT_STR " not found\n");
241 exit(1);
242 }
243
244 if (!strncmp(buffer, DT_CTXT_STR, strlen(DT_CTXT_STR))) {
245 (void)strtok(buffer, DT_CTXT_DELIMITER);
246 ctxt_str = strtok(NULL, DT_CTXT_DELIMITER);
247
248 break;
249 }
250 }
251
252 rewind(Stat_Fp);
253
254 return strtoul(ctxt_str, NULL, 0);
255 }
256
257 /*
258 * Memory allocate and free routines for control blocks (objects) - regular
259 * memory, always zeroed.
260 */
261 void *DT_Mdep_Malloc(size_t l_)
262 {
263 void *rval;
264
265 /*
266 * check memory leaking DT_Mdep_Lock(&Alloc_Count_Lock); alloc_count++;
267 * DT_Mdep_Unlock(&Alloc_Count_Lock);
268 */
269
270 rval = malloc(l_);
271
272 if (rval) {
273 memset(rval, 0, l_);
274 }
275 return (rval);
276 }
277
278 void DT_Mdep_Free(void *a_)
279 {
280 /*
281 * check memory leaking DT_Mdep_Lock(&Alloc_Count_Lock); alloc_count--;
282 * DT_Mdep_Unlock(&Alloc_Count_Lock);
283 */
284
285 free(a_);
286 }
287
288 /*
289 * Lock support
290 *
291 * Lock object constructor
292 */
293 bool DT_Mdep_LockInit(DT_Mdep_LockType * lock_ptr)
294 {
295 return pthread_mutex_init(lock_ptr, 0) ? false : true;
296 }
297
298 /*
299 * Lock object destructor
300 */
301 void DT_Mdep_LockDestroy(DT_Mdep_LockType * lock_ptr)
302 {
303 pthread_mutex_destroy(lock_ptr);
304 }
305
306 /*
307 * Lock
308 */
309 void DT_Mdep_Lock(DT_Mdep_LockType * lock_ptr)
310 {
311 pthread_mutex_lock(lock_ptr);
312 }
313
314 /*
315 * unlock
316 */
317 void DT_Mdep_Unlock(DT_Mdep_LockType * lock_ptr)
318 {
319 pthread_mutex_unlock(lock_ptr);
320 }
321
322 /*
323 * Init Thread Attributes
324 */
325 void DT_Mdep_Thread_Init_Attributes(Thread * thread_ptr)
326 {
327 pthread_attr_init(&thread_ptr->attr);
328 pthread_attr_setstacksize(&thread_ptr->attr, thread_ptr->stacksize);
329 /* Create thread in detached state to free resources on termination;
330 * this precludes doing a pthread_join, but we don't do it
331 */
332 pthread_attr_setdetachstate(&thread_ptr->attr, PTHREAD_CREATE_DETACHED);
333 }
334
335 /*
336 * Destroy Thread Attributes
337 */
338 void DT_Mdep_Thread_Destroy_Attributes(Thread * thread_ptr)
339 {
340 pthread_attr_destroy(&thread_ptr->attr);
341 }
342
343 /*
344 * Start the thread
345 */
346 bool DT_Mdep_Thread_Start(Thread * thread_ptr)
347 {
348 return pthread_create(&thread_ptr->thread_handle,
349 &thread_ptr->attr,
350 DT_Mdep_Thread_Start_Routine, thread_ptr) == 0;
351 }
352
353 /*
354 * Thread execution entry point function
355 */
356 DT_Mdep_Thread_Start_Routine_Return_Type
357 DT_Mdep_Thread_Start_Routine(void *thread_handle)
358 {
359 Thread *thread_ptr;
360 thread_ptr = (Thread *) thread_handle;
361
362 thread_ptr->function(thread_ptr->param);
363 return 0;
364 }
365
366 /*
367 * Thread detach routine. Allows the pthreads
368 * interface to clean up resources properly at
369 * thread's end.
370 */
371 void DT_Mdep_Thread_Detach(DT_Mdep_ThreadHandleType thread_id)
372 { /* AMM */
373 pthread_detach(thread_id);
374 }
375
376 /*
377 * Allows a thread to get its own ID so it
378 * can pass it to routines wanting to act
379 * upon themselves.
380 */
381
382 DT_Mdep_ThreadHandleType DT_Mdep_Thread_SELF(void)
383 { /* AMM */
384 return (pthread_self());
385 }
386
387 /*
388 * Allow a thread to exit and cleanup resources.
389 */
390
391 void DT_Mdep_Thread_EXIT(void *thread_handle)
392 { /* AMM */
393 pthread_exit(thread_handle);
394 }
395
396 /*
397 * DT_Mdep_wait_object_init
398 *
399 * Initialize a wait object
400 *
401 * Input:
402 * wait_obj
403 *
404 * Returns:
405 * 0 if successful
406 * -1 if unsuccessful
407 */
408 int DT_Mdep_wait_object_init(IN DT_WAIT_OBJECT * wait_obj)
409 {
410
411 wait_obj->signaled = DAT_FALSE;
412 if (0 != pthread_cond_init(&wait_obj->cv, NULL)) {
413 return (-1);
414 }
415
416 /* Always returns 0. */
417 pthread_mutex_init(&wait_obj->lock, NULL);
418 return 0;
419 }
420
421 /* Wait on the supplied wait object, up to the specified time_out.
422 * A timeout of DAT_TIMEOUT_INFINITE will wait indefinitely.
423 * Timeout should be specified in micro seconds.
424 *
425 * Functional returns:
426 * 0 -- another thread invoked dapl_os_wait object_wakeup
427 * -1 -- someone else is already waiting in this wait
428 * object.
429 * only one waiter is allowed at a time.
430 * -1 -- another thread invoked dapl_os_wait_object_destroy
431 * -1 -- the specified time limit was reached.
432 */
433
434 int DT_Mdep_wait_object_wait(IN DT_WAIT_OBJECT * wait_obj, IN int timeout_val)
435 {
436 int pthread_status;
437 struct timespec future;
438
439 pthread_status = 0;
440
441 if (timeout_val != DAT_TIMEOUT_INFINITE) {
442 struct timeval now;
443 struct timezone tz;
444 unsigned int microsecs;
445
446 gettimeofday(&now, &tz);
447 microsecs = now.tv_usec + (timeout_val % 1000000);
448 if (microsecs > 1000000) {
449 now.tv_sec = now.tv_sec + timeout_val / 1000000 + 1;
450 now.tv_usec = microsecs - 1000000;
451 } else {
452 now.tv_sec = now.tv_sec + timeout_val / 1000000;
453 now.tv_usec = microsecs;
454 }
455
456 /* Convert timeval to timespec */
457 future.tv_sec = now.tv_sec;
458 future.tv_nsec = now.tv_usec * 1000;
459
460 pthread_mutex_lock(&wait_obj->lock);
461 while (wait_obj->signaled == DAT_FALSE && pthread_status == 0) {
462 pthread_status =
463 pthread_cond_timedwait(&wait_obj->cv,
464 &wait_obj->lock, &future);
465
466 /*
467 * No need to reset &future if we go around the loop;
468 * It's an absolute time.
469 */
470 }
471 /* Reset the signaled status if we were woken up. */
472 if (pthread_status == 0) {
473 wait_obj->signaled = false;
474 }
475 pthread_mutex_unlock(&wait_obj->lock);
476 } else {
477 pthread_mutex_lock(&wait_obj->lock);
478 while (wait_obj->signaled == DAT_FALSE && pthread_status == 0) {
479 pthread_status =
480 pthread_cond_wait(&wait_obj->cv, &wait_obj->lock);
481 }
482 /* Reset the signaled status if we were woken up. */
483 if (pthread_status == 0) {
484 wait_obj->signaled = false;
485 }
486 pthread_mutex_unlock(&wait_obj->lock);
487 }
488
489 if (ETIMEDOUT == pthread_status) {
490 return (-1);
491 } else if (0 != pthread_status) {
492 return (-1);
493 }
494
495 return 0;
496 }
497
498 /*
499 * DT_Mdep_wait_object_wakeup
500 *
501 * Wakeup a thread waiting on a wait object
502 *
503 * Input:
504 * wait_obj
505 *
506 * Returns:
507 * 0 if successful
508 * -1 if not successful
509 */
510 int DT_Mdep_wait_object_wakeup(DT_WAIT_OBJECT * wait_obj)
511 {
512 pthread_mutex_lock(&wait_obj->lock);
513 wait_obj->signaled = true;
514 pthread_mutex_unlock(&wait_obj->lock);
515 if (0 != pthread_cond_signal(&wait_obj->cv)) {
516 return (-1);
517 }
518
519 return 0;
520 }
521
522 /*
523 * DT_Mdep_wait_object_destroy
524 *
525 * Destroy a wait object
526 *
527 * Input:
528 * wait_obj
529 *
530 * Returns:
531 * 0 if successful
532 * -1 if not successful
533 */
534 int DT_Mdep_wait_object_destroy(IN DT_WAIT_OBJECT * wait_obj)
535 {
536 if (0 != pthread_cond_destroy(&wait_obj->cv)) {
537 return (-1);
538 }
539 if (0 != pthread_mutex_destroy(&wait_obj->lock)) {
540 return (-1);
541 }
542
543 return 0;
544 }