Multi-ApplicationOnlineProfiling  2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Utils.h
Go to the documentation of this file.
1 /* ############################ MALP License ############################## */
2 /* # Fri Jan 18 14:00:00 CET 2013 # */
3 /* # Copyright or (C) or Copr. Commissariat a l'Energie Atomique # */
4 /* # # */
5 /* # This software is governed by the CeCILL-C license under French law # */
6 /* # and abiding by the rules of distribution of free software. You can # */
7 /* # use, modify and/ or redistribute the software under the terms of # */
8 /* # the CeCILL-C license as circulated by CEA, CNRS and INRIA at the # */
9 /* # following URL http://www.cecill.info. # */
10 /* # # */
11 /* # The fact that you are presently reading this means that you have # */
12 /* # had knowledge of the CeCILL-C license and that you accept its # */
13 /* # terms. # */
14 /* # # */
15 /* # Authors: # */
16 /* # - BESNARD Jean-Baptiste jean-baptiste.besnard@cea.fr # */
17 /* # # */
18 /* ######################################################################## */
19 
25 #include <sys/stat.h>
26 #include <stdio.h>
27 #include <stdint.h>
28 #include <unistd.h>
29 
30 #ifndef TRACELIB_UTILS_H
31 #define TRACELIB_UTILS_H
32 
38 static inline int MALP_Trace_check_dir(const char *path)
39 {
40  struct stat statb;
41 
42  if (lstat(path, &statb) == -1) {
43  return 0;
44  }
45 
46  if (S_ISDIR(statb.st_mode)) {
47  return 1;
48  }
49 
50  return 0;
51 }
52 
57 static inline void MALP_Trace_try_mkdir( char *path )
58 {
59  if (mkdir(path, S_IRWXU) < 0) {
60  if (!MALP_Trace_check_dir(path)) {
61  printf("Failed to make directory : %s \n", path);
62  abort();
63  }
64  }
65 }
66 
72 static inline int MALP_Trace_file_exists( const char *path )
73 {
74  struct stat statb;
75 
76  if (lstat(path, &statb) == -1) {
77  return 0;
78  }
79 
80  return 1;
81 }
82 
89 static inline int MALP_Trace_fast_strcmp( char *haystack, char *needle )
90 {
91  if ( !haystack || !needle )
92  return 0;
93 
94  while ( (*haystack != '\0') && (*needle !='\0') ) {
95  if ( *haystack != *needle )
96  return 0;
97 
98  haystack++;
99  needle++;
100  }
101 
102  if ( *haystack != *needle )
103  return 0;
104 
105  return 1;
106 }
107 
113 static inline int MALP_Trace_is_numeric(char *number)
114 {
115  while (*number != '\0' ) {
116  if ( (*number < 48 || 57 < *number) && ( *number != '.') )
117  return 0;
118 
119  number++;
120  }
121  return 1;
122 }
123 
129 static inline int MALP_Trace_line_begins_with_sharp(char *line)
130 {
131  if ( !line )
132  return 1;
133 
134  char *tmp = line;
135  while (*tmp != '\0') {
136 
137  if (*tmp != '#' && *tmp != ' ')
138  return 0;
139 
140  if (*tmp == '#' )
141  return 1;
142 
143  tmp++;
144  }
145 
146  return 0;
147 }
148 
153 static inline uint64_t MALP_Trace_total_mem()
154 {
155  return (sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE) );
156 }
157 
162 static inline uint64_t MALP_Trace_mem_per_core()
163 {
164  int core_count = sysconf(_SC_NPROCESSORS_CONF);
165  return MALP_Trace_total_mem()/core_count;
166 }
167 
173 static inline char * my_pad( char *val )
174 {
175 
176  int len;
177  len = strlen(val);
178 
179  if( len == 0 )
180  return val;
181 
182 
183  while( val[len - 1] == ' ' )
184  {
185  val[len - 1] = '\0';
186  len--;
187  }
188 
189  while( *val == ' ' )
190  val++;
191 
192  return val;
193 }
194 
195 
196 #endif
197 
static char * my_pad(char *val)
Removes starting and ending spaces from a string.
Definition: Utils.h:173
static int MALP_Trace_file_exists(const char *path)
Checks if a file exists.
Definition: Utils.h:72
static void MALP_Trace_try_mkdir(char *path)
Creates a directory without failing if it already exists.
Definition: Utils.h:57
static int MALP_Trace_check_dir(const char *path)
Checks if a path is a directory.
Definition: Utils.h:38
static int MALP_Trace_line_begins_with_sharp(char *line)
Checks if the first not space char of a string is a #.
Definition: Utils.h:129
static uint64_t MALP_Trace_mem_per_core()
Getter on the per core memory on the current node.
Definition: Utils.h:162
static int MALP_Trace_is_numeric(char *number)
Checks if a string contains a numerical element.
Definition: Utils.h:113
static int MALP_Trace_fast_strcmp(char *haystack, char *needle)
Fast string comparison.
Definition: Utils.h:89
static uint64_t MALP_Trace_total_mem()
Getter on the total amount of memory available on current node.
Definition: Utils.h:153