codemod 2010-2016 to 2010-present
[hiphop-php.git] / hphp / runtime / base / apc-file-storage.h
blobf9d02d7ea96f1aaf1bec89a1d5864697e1246016
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 +----------------------------------------------------------------------+
17 #ifndef incl_HPHP_APC_FILE_STORAGE_H_
18 #define incl_HPHP_APC_FILE_STORAGE_H_
20 #include <atomic>
21 #include <string>
22 #include <mutex>
23 #include <vector>
25 #include "hphp/util/hash.h"
27 namespace HPHP {
29 //////////////////////////////////////////////////////////////////////
32 * To save memory, hhvm puts portions of primed APC data in a file-backed mmap
33 * that we can madvise away after an initial warmup period.
35 struct APCFileStorage {
36 enum class StorageState {
37 Invalid,
38 Open,
39 Sealed,
40 Full
43 APCFileStorage() = default;
44 APCFileStorage(const APCFileStorage&) = delete;
45 APCFileStorage& operator=(const APCFileStorage&) = delete;
47 void enable(const std::string& prefix, size_t chunkSize);
48 char *put(const char *data, uint32_t len);
49 void seal();
50 void adviseOut();
51 bool hashCheck();
52 void cleanup();
53 StorageState getState() { return m_state; }
55 private:
56 bool addFile();
58 private:
59 // [32-bit chunk index]:[32-bit offset]
60 std::atomic_uint_fast64_t m_current{0};
61 size_t m_chunkSize{0};
62 StorageState m_state{StorageState::Invalid};
63 std::vector<char*> m_chunks;
64 std::vector<std::string> m_fileNames;
65 std::vector<int> m_fds;
66 std::string m_prefix;
68 // This lock is needed when manipulating chunks.
69 std::mutex m_lock;
70 static constexpr strhash_t TombHash = 0xdeadbeef;
71 static constexpr uint32_t PaddingSize = sizeof(strhash_t) + // hash
72 sizeof(int32_t) + // len
73 sizeof(char); // '\0'
76 extern APCFileStorage s_apc_file_storage;
78 //////////////////////////////////////////////////////////////////////
82 #endif