PcapPlusPlus  20.08
SystemUtils.h
Go to the documentation of this file.
1 #ifndef PCAPPP_SYSTEM_UTILS
2 #define PCAPPP_SYSTEM_UTILS
3 
4 #include <stdint.h>
5 #include <string>
6 #include <vector>
7 #if defined(WIN32) || defined(WINx64) || defined(PCAPPP_MINGW_ENV)
8 #include <windows.h>
9 #else
10 #include <pthread.h>
11 #endif
12 
14 
15 #define MAX_NUM_OF_CORES 32
16 
17 #ifdef _MSC_VER
18 int gettimeofday(struct timeval * tp, struct timezone * tzp);
19 #endif
20 
25 namespace pcpp
26 {
27 
32  struct SystemCore
33  {
39  uint32_t Mask;
40 
44  uint8_t Id;
45 
50  bool operator==(const SystemCore& other) const { return Id == other.Id; }
51  };
52 
57  struct SystemCores
58  {
62  static const SystemCore Core0;
66  static const SystemCore Core1;
70  static const SystemCore Core2;
74  static const SystemCore Core3;
78  static const SystemCore Core4;
82  static const SystemCore Core5;
86  static const SystemCore Core6;
90  static const SystemCore Core7;
94  static const SystemCore Core8;
98  static const SystemCore Core9;
102  static const SystemCore Core10;
106  static const SystemCore Core11;
110  static const SystemCore Core12;
114  static const SystemCore Core13;
118  static const SystemCore Core14;
122  static const SystemCore Core15;
126  static const SystemCore Core16;
130  static const SystemCore Core17;
134  static const SystemCore Core18;
138  static const SystemCore Core19;
142  static const SystemCore Core20;
146  static const SystemCore Core21;
150  static const SystemCore Core22;
154  static const SystemCore Core23;
158  static const SystemCore Core24;
162  static const SystemCore Core25;
166  static const SystemCore Core26;
170  static const SystemCore Core27;
174  static const SystemCore Core28;
178  static const SystemCore Core29;
182  static const SystemCore Core30;
186  static const SystemCore Core31;
187 
191  static const SystemCore IdToSystemCore[MAX_NUM_OF_CORES];
192  };
193 
194  typedef uint32_t CoreMask;
195 
200  int getNumOfCores();
201 
207 
208 
214  CoreMask createCoreMaskFromCoreVector(std::vector<SystemCore> cores);
215 
216 
222  CoreMask createCoreMaskFromCoreIds(std::vector<int> coreIds);
223 
224 
230  void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector<SystemCore>& resultVec);
231 
237  std::string executeShellCommand(const std::string command);
238 
244  bool directoryExists(std::string dirPath);
245 
253  int clockGetTime(long& sec, long& nsec);
254 
260  class AppName
261  {
262  private:
263  static std::string m_AppName;
264 
265  public:
271  static void init(int argc, char* argv[])
272  {
273  if (argc == 0)
274  {
275  m_AppName.clear();
276  return;
277  }
278 
279  m_AppName = argv[0];
280 
281  // remove Linux/Unix path
282  size_t lastPos = m_AppName.rfind('/');
283  if (lastPos != std::string::npos)
284  {
285  m_AppName = m_AppName.substr(lastPos + 1);
286  }
287 
288  // remove Windows path
289  lastPos = m_AppName.rfind('\\');
290  if (lastPos != std::string::npos)
291  {
292  m_AppName = m_AppName.substr(lastPos + 1);
293  }
294 
295  // remove file extension
296  m_AppName = m_AppName.substr(0, m_AppName.rfind('.'));
297  }
298 
302  static const std::string& get() { return m_AppName; }
303  };
304 
311  {
312  public:
318  typedef void (*EventHandlerCallback)(void* cookie);
319 
325  {
326  static ApplicationEventHandler instance;
327  return instance;
328  }
329 
336  void onApplicationInterrupted(EventHandlerCallback handler, void* cookie);
337 
338  private:
339  EventHandlerCallback m_ApplicationInterruptedHandler;
340  void* m_ApplicationInterruptedCookie;
341 
342  // private c'tor
344 
345 #ifdef WIN32
346  static BOOL WINAPI handlerRoutine(DWORD fdwCtrlType);
347 #else
348  pthread_mutex_t m_HandlerRoutineMutex;
349  static void handlerRoutine(int signum);
350 #endif
351  };
352 
353 } // namespace pcpp
354 
355 #endif /* PCAPPP_SYSTEM_UTILS */
pcpp::SystemCore
Definition: SystemUtils.h:32
pcpp::SystemCore::Mask
uint32_t Mask
Definition: SystemUtils.h:39
pcpp::getNumOfCores
int getNumOfCores()
pcpp::SystemCores::Core24
static const SystemCore Core24
Definition: SystemUtils.h:158
pcpp::ApplicationEventHandler::getInstance
static ApplicationEventHandler & getInstance()
Definition: SystemUtils.h:324
pcpp::SystemCores::Core17
static const SystemCore Core17
Definition: SystemUtils.h:130
pcpp::SystemCores::Core12
static const SystemCore Core12
Definition: SystemUtils.h:110
pcpp::SystemCores::Core5
static const SystemCore Core5
Definition: SystemUtils.h:82
pcpp::ApplicationEventHandler
Definition: SystemUtils.h:310
pcpp::SystemCores::Core19
static const SystemCore Core19
Definition: SystemUtils.h:138
pcpp::executeShellCommand
std::string executeShellCommand(const std::string command)
pcpp::SystemCores::Core29
static const SystemCore Core29
Definition: SystemUtils.h:178
pcpp::createCoreVectorFromCoreMask
void createCoreVectorFromCoreMask(CoreMask coreMask, std::vector< SystemCore > &resultVec)
pcpp::AppName::init
static void init(int argc, char *argv[])
Definition: SystemUtils.h:271
pcpp::SystemCores::Core18
static const SystemCore Core18
Definition: SystemUtils.h:134
pcpp::SystemCores::Core16
static const SystemCore Core16
Definition: SystemUtils.h:126
pcpp::SystemCores::Core4
static const SystemCore Core4
Definition: SystemUtils.h:78
pcpp::clockGetTime
int clockGetTime(long &sec, long &nsec)
pcpp::SystemCores::Core9
static const SystemCore Core9
Definition: SystemUtils.h:98
pcpp::SystemCores::Core21
static const SystemCore Core21
Definition: SystemUtils.h:146
pcpp::ApplicationEventHandler::EventHandlerCallback
void(* EventHandlerCallback)(void *cookie)
Definition: SystemUtils.h:318
pcpp::createCoreMaskFromCoreVector
CoreMask createCoreMaskFromCoreVector(std::vector< SystemCore > cores)
pcpp::SystemCores::Core0
static const SystemCore Core0
Definition: SystemUtils.h:62
pcpp::SystemCores::Core30
static const SystemCore Core30
Definition: SystemUtils.h:182
pcpp::AppName
Definition: SystemUtils.h:260
pcpp::SystemCores::Core22
static const SystemCore Core22
Definition: SystemUtils.h:150
pcpp::SystemCores::Core14
static const SystemCore Core14
Definition: SystemUtils.h:118
pcpp::SystemCores::IdToSystemCore
static const SystemCore IdToSystemCore[32]
Definition: SystemUtils.h:191
pcpp::SystemCores::Core25
static const SystemCore Core25
Definition: SystemUtils.h:162
pcpp::SystemCores::Core7
static const SystemCore Core7
Definition: SystemUtils.h:90
pcpp
The main namespace for the PcapPlusPlus lib.
pcpp::directoryExists
bool directoryExists(std::string dirPath)
pcpp::SystemCores::Core23
static const SystemCore Core23
Definition: SystemUtils.h:154
pcpp::SystemCores
Definition: SystemUtils.h:57
pcpp::SystemCores::Core1
static const SystemCore Core1
Definition: SystemUtils.h:66
pcpp::SystemCores::Core15
static const SystemCore Core15
Definition: SystemUtils.h:122
pcpp::ApplicationEventHandler::onApplicationInterrupted
void onApplicationInterrupted(EventHandlerCallback handler, void *cookie)
pcpp::SystemCores::Core13
static const SystemCore Core13
Definition: SystemUtils.h:114
pcpp::SystemCores::Core2
static const SystemCore Core2
Definition: SystemUtils.h:70
pcpp::SystemCores::Core3
static const SystemCore Core3
Definition: SystemUtils.h:74
pcpp::SystemCore::Id
uint8_t Id
Definition: SystemUtils.h:44
pcpp::SystemCores::Core26
static const SystemCore Core26
Definition: SystemUtils.h:166
pcpp::SystemCores::Core11
static const SystemCore Core11
Definition: SystemUtils.h:106
pcpp::createCoreMaskFromCoreIds
CoreMask createCoreMaskFromCoreIds(std::vector< int > coreIds)
pcpp::getCoreMaskForAllMachineCores
CoreMask getCoreMaskForAllMachineCores()
pcpp::SystemCores::Core6
static const SystemCore Core6
Definition: SystemUtils.h:86
pcpp::SystemCores::Core20
static const SystemCore Core20
Definition: SystemUtils.h:142
pcpp::SystemCores::Core28
static const SystemCore Core28
Definition: SystemUtils.h:174
pcpp::SystemCore::operator==
bool operator==(const SystemCore &other) const
Definition: SystemUtils.h:50
pcpp::SystemCores::Core27
static const SystemCore Core27
Definition: SystemUtils.h:170
pcpp::SystemCores::Core8
static const SystemCore Core8
Definition: SystemUtils.h:94
pcpp::SystemCores::Core10
static const SystemCore Core10
Definition: SystemUtils.h:102
pcpp::SystemCores::Core31
static const SystemCore Core31
Definition: SystemUtils.h:186
pcpp::AppName::get
static const std::string & get()
Definition: SystemUtils.h:302