codemod 2010-2016 to 2010-present
[hiphop-php.git] / hphp / util / shm-counter.cpp
blob84949a79b32b4c7839dce2d598d22cde72d4c1da
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #include "hphp/util/shm-counter.h"
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <new>
23 #ifdef ENABLE_SHM_COUNTER
24 #include <sys/ipc.h>
25 #include <sys/shm.h>
26 #endif
28 namespace HPHP {
29 ///////////////////////////////////////////////////////////////////////////////
31 bool ShmCounters::created;
32 int ShmCounters::shmid;
33 ShmCounters::logError_t ShmCounters::logError;
34 ShmCounters *ShmCounters::s_shmCounters;
36 #define LOG_ERROR(fmt, ...) \
37 do { \
38 if (ShmCounters::logError) { \
39 ShmCounters::logError(fmt, ##__VA_ARGS__); \
40 } else { \
41 fprintf(stderr, fmt, ##__VA_ARGS__); \
42 } \
43 } while (false)
45 ShmCounters::~ShmCounters() {
46 #ifdef ENABLE_SHM_COUNTER
47 if (!ShmCounters::created) return;
48 if (shmdt(s_shmCounters) == -1) {
49 LOG_ERROR("shmdt failed: %d\n", errno);
50 return;
52 struct shmid_ds sb;
53 if (shmctl(ShmCounters::shmid, IPC_STAT, &sb) == -1) {
54 LOG_ERROR("shmctl failed: %d\n", errno);
55 return;
57 if (sb.shm_nattch == 0) shmctl(shmid, IPC_RMID, 0);
58 #endif
61 bool ShmCounters::initialize(bool create, logError_t logError /* = NULL */) {
62 #ifdef ENABLE_SHM_COUNTER
63 ShmCounters::logError = logError;
64 int flags = 0666;
65 if (create) flags |= IPC_CREAT;
66 int shmid = shmget(SHM_COUNTER_KEY, sizeof(ShmCounters), flags);
67 if (shmid == -1) {
68 LOG_ERROR("shmget failed: %d\n", errno);
69 return false;
71 struct shmid_ds sb;
72 if (shmctl(shmid, IPC_STAT, &sb) == -1) {
73 LOG_ERROR("shmctl failed: %d\n", errno);
74 return false;
76 if (sb.shm_nattch == 0 && !create) {
77 LOG_ERROR("no process attached, exiting...\n");
78 shmctl(shmid, IPC_RMID, 0);
79 return false;
81 s_shmCounters = (ShmCounters *)shmat(shmid, 0, 0);
82 if (s_shmCounters == (void *)-1) {
83 LOG_ERROR("shmat failed: %d\n", errno);
84 return false;
86 if (create) new (s_shmCounters) ShmCounters();
87 ShmCounters::created = create;
88 ShmCounters::shmid = shmid;
89 #endif
90 return true;
93 void ShmCounter::dump() {
94 fprintf(stderr, "%s:\t%lld\n", name, count);
97 void ShmCounters::dump() {
98 if (s_shmCounters == nullptr) return;
99 for (ShmCounter *cp = &s_shmCounters->dummy_def1;
100 cp < (ShmCounter *)&s_shmCounters->dummy_defmax;
101 cp++) {
102 cp->dump();
106 ///////////////////////////////////////////////////////////////////////////////