FlashGraph-ng
A new frontier in large-scale graph analysis and data mining
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Pages
common_c.h
1 #ifndef __COMMON_C_H__
2 #define __COMMON_C_H__
3 
4 /*
5  * Copyright 2014 Open Connectome Project (http://openconnecto.me)
6  * Written by Da Zheng (zhengda1936@gmail.com)
7  *
8  * This file is part of SAFSlib.
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include <errno.h>
24 #include <sys/time.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/syscall.h>
29 #include <numa.h>
30 #include <assert.h>
31 #include <execinfo.h>
32 #include <unistd.h>
33 
34 #define gettid() syscall(__NR_gettid)
35 
36 #define ROUND(off, base) (((long) off) & (~((long) (base) - 1)))
37 #define ROUNDUP(off, base) (((long) off + (base) - 1) & (~((long) (base) - 1)))
38 
39 #define PRINT_BACKTRACE() \
40  do { \
41  void *buf[100]; \
42  char **strings; \
43  int nptrs = backtrace(buf, 100); \
44  strings = backtrace_symbols(buf, nptrs); \
45  if (strings == NULL) { \
46  perror("backtrace_symbols"); \
47  exit(EXIT_FAILURE); \
48  } \
49  for (int i = 0; i < nptrs; i++) { \
50  char syscom[256]; \
51  printf("[bt] #%d %s\n", i, strings[i]); \
52  sprintf(syscom,"addr2line %p -e %s", buf[i], program_invocation_name);\
53  int ret __attribute__((unused)) = system(syscom); \
54  } \
55  free(strings); \
56  } while (0)
57 
58 #define ASSERT_TRUE(x) \
59  if (!(x)) { \
60  PRINT_BACKTRACE(); \
61  assert(x); \
62  }
63 
64 #ifdef __cplusplus
65 extern "C" {
66 #endif
67 
68 inline static float time_diff(struct timeval time1, struct timeval time2)
69 {
70  return time2.tv_sec - time1.tv_sec
71  + ((float)(time2.tv_usec - time1.tv_usec))/1000000;
72 }
73 
74 inline static long time_diff_us(struct timeval time1, struct timeval time2)
75 {
76  return (time2.tv_sec - time1.tv_sec) * 1000000
77  + (time2.tv_usec - time1.tv_usec);
78 }
79 
80 inline static long min(long v1, long v2)
81 {
82  return v1 > v2 ? v2 : v1;
83 }
84 
85 inline static long max(long v1, long v2)
86 {
87  return v1 < v2 ? v2 : v1;
88 }
89 
90 inline static int power2(int num)
91 {
92  if (num < 0)
93  num = -num;
94  if (num == 1)
95  return 1;
96  while (num > 1) {
97  if (num % 2)
98  return 0;
99  num = num / 2;
100  }
101  return 1;
102 }
103 
104 inline static long get_curr_ms()
105 {
106  struct timeval tv;
107  gettimeofday(&tv, NULL);
108  return ((long) tv.tv_sec) * 1000 + tv.tv_usec / 1000;
109 };
110 
111 inline static long get_curr_us()
112 {
113  struct timeval tv;
114  gettimeofday(&tv, NULL);
115  return ((long) tv.tv_sec) * 1000 * 1000 + tv.tv_usec;
116 }
117 
118 int isnumeric(char *str);
119 
120 static const int CONST_A = 27644437;
121 static const long CONST_P = 68718952447L;
122 
123 static inline int universal_hash(off_t v, int modulo)
124 {
125  return (v * CONST_A) % CONST_P % modulo;
126 }
127 
128 /*
129  * These two functions should be used to allocate/free large chunks of memory.
130  */
131 void set_use_huge_page(bool v);
132 void *malloc_large(size_t size);
133 void free_large(void *addr, size_t size);
134 
135 #define likely(x) __builtin_expect((x),1)
136 #define unlikely(x) __builtin_expect((x),0)
137 
138 #ifdef __cplusplus
139 }
140 #endif
141 
142 #endif