Add a HHIR-level peephole optimization to reorder CheckTypes
[hiphop-php.git] / hphp / util / shm-counter.cpp
blob056c43d8d949a42855e59926eb06e41f9a042cbb
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*/,
62 logError_t /*logError*/ /* = NULL */) {
63 #ifdef ENABLE_SHM_COUNTER
64 ShmCounters::logError = logError;
65 int flags = 0666;
66 if (create) flags |= IPC_CREAT;
67 int shmid = shmget(SHM_COUNTER_KEY, sizeof(ShmCounters), flags);
68 if (shmid == -1) {
69 LOG_ERROR("shmget failed: %d\n", errno);
70 return false;
72 struct shmid_ds sb;
73 if (shmctl(shmid, IPC_STAT, &sb) == -1) {
74 LOG_ERROR("shmctl failed: %d\n", errno);
75 return false;
77 if (sb.shm_nattch == 0 && !create) {
78 LOG_ERROR("no process attached, exiting...\n");
79 shmctl(shmid, IPC_RMID, 0);
80 return false;
82 s_shmCounters = (ShmCounters *)shmat(shmid, 0, 0);
83 if (s_shmCounters == (void *)-1) {
84 LOG_ERROR("shmat failed: %d\n", errno);
85 return false;
87 if (create) new (s_shmCounters) ShmCounters();
88 ShmCounters::created = create;
89 ShmCounters::shmid = shmid;
90 #endif
91 return true;
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;
102 cp++) {
103 cp->dump();
107 ///////////////////////////////////////////////////////////////////////////////