2 +----------------------------------------------------------------------+
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"
23 #ifdef ENABLE_SHM_COUNTER
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, ...) \
38 if (ShmCounters::logError) { \
39 ShmCounters::logError(fmt, ##__VA_ARGS__); \
41 fprintf(stderr, fmt, ##__VA_ARGS__); \
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
);
53 if (shmctl(ShmCounters::shmid
, IPC_STAT
, &sb
) == -1) {
54 LOG_ERROR("shmctl failed: %d\n", errno
);
57 if (sb
.shm_nattch
== 0) shmctl(shmid
, IPC_RMID
, 0);
61 bool ShmCounters::initialize(bool /*create*/,
62 logError_t
/*logError*/ /* = NULL */) {
63 #ifdef ENABLE_SHM_COUNTER
64 ShmCounters::logError
= logError
;
66 if (create
) flags
|= IPC_CREAT
;
67 int shmid
= shmget(SHM_COUNTER_KEY
, sizeof(ShmCounters
), flags
);
69 LOG_ERROR("shmget failed: %d\n", errno
);
73 if (shmctl(shmid
, IPC_STAT
, &sb
) == -1) {
74 LOG_ERROR("shmctl failed: %d\n", errno
);
77 if (sb
.shm_nattch
== 0 && !create
) {
78 LOG_ERROR("no process attached, exiting...\n");
79 shmctl(shmid
, IPC_RMID
, 0);
82 s_shmCounters
= (ShmCounters
*)shmat(shmid
, 0, 0);
83 if (s_shmCounters
== (void *)-1) {
84 LOG_ERROR("shmat failed: %d\n", errno
);
87 if (create
) new (s_shmCounters
) ShmCounters();
88 ShmCounters::created
= create
;
89 ShmCounters::shmid
= shmid
;
94 void ShmCounter::dump() {
95 fprintf(stderr
, "%s:\t%lld\n", name
, count
);
98 void ShmCounters::dump() {
99 if (s_shmCounters
== nullptr) return;
100 for (ShmCounter
*cp
= &s_shmCounters
->dummy_def1
;
101 cp
< (ShmCounter
*)&s_shmCounters
->dummy_defmax
;
107 ///////////////////////////////////////////////////////////////////////////////