KTH framework for Nek5000 toolboxes; testing version  0.0.1
chelpers.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/resource.h>
4 #include <string.h>
5 #include "name.h"
6 
7 
8 #define cexit FORTRAN_UNPREFIXED(cexit, CEXIT)
9 #define print_stack FORTRAN_UNPREFIXED(print_stack, PRINT_STACK)
10 #define sizeOfLongInt FORTRAN_UNPREFIXED(sizeoflongint, SIZEOFLONGINT)
11 #define getmaxrss FORTRAN_UNPREFIXED(getmaxrss, GETMAXRSS)
12 #define set_stdout FORTRAN_UNPREFIXED(set_stdout, SET_STDOUT)
13 
14 #if defined __GLIBC__
15 
16 #include <execinfo.h>
17 /* Obtain a backtrace and print it to stdout. */
18 void print_stack(void)
19 {
20  void *bt[50];
21  int i;
22  int bt_size = backtrace(bt, 50);
23  char **symbols = backtrace_symbols(bt, bt_size);
24 
25  printf ("backtrace(): obtained %d stack frames.\n", bt_size);
26  for (i=0; i<bt_size; i++) printf("%s\n", symbols[i]);
27  free (symbols);
28 }
29 #else
30 void print_stack(){};
31 #endif
32 
33 double getmaxrss()
34 {
35  struct rusage r_usage;
36 
37  getrusage(RUSAGE_SELF,&r_usage);
38 #if defined(__APPLE__) && defined(__MACH__)
39  return (double)r_usage.ru_maxrss;
40 #else
41  return (double)(r_usage.ru_maxrss * 1024L);
42 #endif
43 }
44 
45 int sizeOfLongInt()
46 {
47  return sizeof(long int);
48 }
49 
50 void set_stdout(char *f, int *sid, int flen)
51 {
52  char *logfile = (char *) malloc((flen+2+5+1)*sizeof(char));
53  strncpy(logfile, f, flen);
54  int i;
55  for (i=flen-1; i>=0; i--) if (logfile[i] != ' ') break;
56  logfile[i+1] = '\0';
57 
58  int redirect = 0;
59  char *envvar;
60 
61  if (logfile[0] != '\0') {
62  redirect = 1;
63  }
64  else if (envvar = getenv("NEK_LOGFILE")) {
65  if (*sid >= 0) sprintf(logfile, "s%05d_", *sid);
66  strcat(logfile + strlen(logfile), envvar);
67  redirect = 1;
68  }
69 
70  if (redirect) {
71  printf("redirecting stdout to %s\n",logfile);
72  freopen(logfile, "w+", stdout);
73  }
74 }
75 
76 void cexit(int *ierr)
77 {
78  exit(*ierr);
79 }
#define sizeOfLongInt
Definition: chelpers.c:10
#define cexit
Definition: chelpers.c:8
#define print_stack
Definition: chelpers.c:9
#define set_stdout
Definition: chelpers.c:12
#define getmaxrss
Definition: chelpers.c:11