{"action":"created","comment":{"url":"https://api.github.com/repos/cuberite/cuberite/pulls/comments/63171558","id":63171558,"diff_hunk":"@@ -0,0 +1,68 @@\n+#include \"Globals.h\"  // NOTE: MSVC stupidness requires this to be the same across all modules\n+#include \"MemoryCounter.h\"\n+\n+\n+\n+\n+\n+cMemoryCounter::cMemoryCounter() :\n+\tm_ApproximateChunkRamSize(0),\n+\tm_CanUnloadCount(0)\n+{\n+\n+}\n+\n+\n+\n+\n+\n+void cMemoryCounter::IncrementApproximateChunkRAM(size_t a_Increment)\n+{\n+\tm_ApproximateChunkRamSize += a_Increment;\n+}\n+\n+\n+\n+\n+\n+void cMemoryCounter::DecrementApproximateChunkRAM(size_t a_Decrement)\n+{\n+\tASSERT(m_ApproximateChunkRamSize >= a_Decrement);\n+\tm_ApproximateChunkRamSize -= a_Decrement;\n+}\n+\n+\n+\n+\n+/** Increments the counter of the amount of unused chunks our world has by one. */\n+void cMemoryCounter::IncrementCanUnloadCount()\n+{\n+\t++m_CanUnloadCount;\n+}\n+\n+\n+\n+\n+\n+void cMemoryCounter::DecrementCanUnloadCount()\n+{\n+\t--m_CanUnloadCount;\n+}\n+\n+\n+\n+\n+\n+size_t cMemoryCounter::GetCanUnloadCount()\n+{\n+\treturn m_CanUnloadCount;\n+}\n+\n+\n+\n+\n+\n+size_t cMemoryCounter::GetApproximateChunkRAM()\n+{\n+\treturn static_cast<size_t>(m_ApproximateChunkRamSize / 1048576);  // Cast to MiB","path":"src/MemoryCounter.cpp","position":67,"original_position":67,"commit_id":"d1e8568ea3558d55fa76ce805ccc862ce8cdea25","original_commit_id":"d1e8568ea3558d55fa76ce805ccc862ce8cdea25","user":{"login":"SphinxC0re","id":3702016,"avatar_url":"https://avatars.githubusercontent.com/u/3702016?v=3","gravatar_id":"","url":"https://api.github.com/users/SphinxC0re","html_url":"https://github.com/SphinxC0re","followers_url":"https://api.github.com/users/SphinxC0re/followers","following_url":"https://api.github.com/users/SphinxC0re/following{/other_user}","gists_url":"https://api.github.com/users/SphinxC0re/gists{/gist_id}","starred_url":"https://api.github.com/users/SphinxC0re/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SphinxC0re/subscriptions","organizations_url":"https://api.github.com/users/SphinxC0re/orgs","repos_url":"https://api.github.com/users/SphinxC0re/repos","events_url":"https://api.github.com/users/SphinxC0re/events{/privacy}","received_events_url":"https://api.github.com/users/SphinxC0re/received_events","type":"User","site_admin":false},"body":"We should really introduce memory size literals like `B`, `MB`, `GB`, etc...","created_at":"2016-05-13T11:59:13Z","updated_at":"2016-05-13T11:59:13Z","html_url":"https://github.com/cuberite/cuberite/pull/3151#discussion_r63171558","pull_request_url":"https://api.github.com/repos/cuberite/cuberite/pulls/3151","_links":{"self":{"href":"https://api.github.com/repos/cuberite/cuberite/pulls/comments/63171558"},"html":{"href":"https://github.com/cuberite/cuberite/pull/3151#discussion_r63171558"},"pull_request":{"href":"https://api.github.com/repos/cuberite/cuberite/pulls/3151"}}},"pull_request":{"url":"https://api.github.com/repos/cuberite/cuberite/pulls/3151","id":67050439,"html_url":"https://github.com/cuberite/cuberite/pull/3151","diff_url":"https://github.com/cuberite/cuberite/pull/3151.diff","patch_url":"https://github.com/cuberite/cuberite/pull/3151.patch","issue_url":"https://api.github.com/repos/cuberite/cuberite/issues/3151","number":3151,"state":"open","locked":false,"title":"Smart chunk unloading","user":{"login":"LogicParrot","id":5030488,"avatar_url":"https://avatars.githubusercontent.com/u/5030488?v=3","gravatar_id":"","url":"https://api.github.com/users/LogicParrot","html_url":"https://github.com/LogicParrot","followers_url":"https://api.github.com/users/LogicParrot/followers","following_url":"https://api.github.com/users/LogicParrot/following{/other_user}","gists_url":"https://api.github.com/users/LogicParrot/gists{/gist_id}","starred_url":"https://api.github.com/users/LogicParrot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LogicParrot/subscriptions","organizations_url":"https://api.github.com/users/LogicParrot/orgs","repos_url":"https://api.github.com/users/LogicParrot/repos","events_url":"https://api.github.com/users/LogicParrot/events{/privacy}","received_events_url":"https://api.github.com/users/LogicParrot/received_events","type":"User","site_admin":false},"body":"Work in progress. This is a smarter replacement for #3142\r\n\r\n- The server has  a `MaxChunkRAM` setting, configurable in `settings.ini` (default: 30 MiB)\r\n- The server has a `MinimizeRam` setting, configurable in `settings.ini` (default: 1)\r\n\r\n**Terminology:**\r\nDirtiness:\r\n- Clean chunk: No changes were made, no need to save to disk before unloading.\r\n- Dirty chunk: A chunk which underwent changes. Must save to disk before unloading.\r\n\r\nUsage:\r\n\r\n- Unused chunk: A chunk with no players (nor chunkStay, etc.) nearby, the server may unload this.\r\n- Used chunk: A chunk which is actively used by the game. The server must not unload this.\r\n\r\nOther:\r\n\r\n- Saving a chunk: Turns dirty chunks into clean chunks by saving them to disk. After a save, the chunk is no longer dirty and the server may unload it.\r\n- Unloading a chunk: Removing a chunk from RAM and freeing that RAM. We can only unload chunks that are both unused and clean.\r\n\r\nThe server has two saving/unloading strategies.\r\n\r\n**Strategy 1: `MinimizeRam` is 0:**\r\n The server is RAM-greedy, and uses all the RAM specified in `MaxChunkRAM` as a chunk cache in order to improve performance. Chunks are saved/unloaded as late as possible. Disk I/O is significantly reduced. Performance is improved. RAM usage is typically static and will not go below or above  `MaxChunkRAM`.\r\n - Dirty chunks are saved in 5 minute intervals to avoid data loss in case of a crash.\r\n - If current RAM usage exceeds `MaxChunkRAM`, a minimum amount of clean unused chunks is unloaded in order to preserve the `MaxChunkRAM` limit. The chunks are chosen randomly.\r\n - If there are no clean unused chunks left to unload and `MaxChunkRAM` still cannot be maintained, a save cycle is forced, allowing more chunks to be unloaded.\r\n - Other than the above three scenarios, chunks are never unloaded or saved.\r\n - The amount of RAM used by chunks is normally static and equals `MaxChunkRAM`\r\n - If the activity is too high and the `MaxChunkRAM` limit cannot be maintained even after all measures were taken, the \"agressive\" strategy is used. This happens when the RAM required by the **used chunks** exceeds `MaxChunkRAM`.\r\n\r\n**Strategy 2: `MinimizeRam` is 1:**\r\n The server is RAM-economic, attempts to never reach `MaxChunkRAM`. Clean unused Chunks are unloaded as soon as possible.\r\n - Dirty chunks are saved in 5 minute intervals to avoid data loss in case of a crash.\r\n - Clean unused chunks are unloaded as soon as possible, making `MaxChunkRAM` much harder to reach in comparison with the previous scheme.\r\n - If current RAM usage exceeds `MaxChunkRAM`, a save cycle is forced, allowing more chunks to be unloaded.\r\n - Other than the three above scenarios, chunks are never unloaded or saved.\r\n - If the activity is too high and the `MaxChunkRAM` limit cannot be maintained, the \"agressive\" strategy is used. This happens when the RAM required by the **used chunks** exceeds `MaxChunkRAM`.\r\n\r\n**\"Agressive\" strategy**:\r\n - Dirty chunks are saved in 5 minute intervals to avoid data loss in case of a crash.\r\n - Whenever a chunk becomes unused, it is unloaded asap (and saved if dirty).\r\n - The agressive strategy is technically nonexistent and does not have its own code. It's just what happens when the server is constantly above `MaxChunkRAM` in either strategy.\r\n\r\nAdditionally, this closes #3166 ","created_at":"2016-04-19T16:36:30Z","updated_at":"2016-05-13T11:59:13Z","closed_at":null,"merged_at":null,"merge_commit_sha":"861310be1faa7e1bbff22c3bee0f95c89a7bfe00","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/cuberite/cuberite/pulls/3151/commits","review_comments_url":"https://api.github.com/repos/cuberite/cuberite/pulls/3151/comments","review_comment_url":"https://api.github.com/repos/cuberite/cuberite/pulls/comments{/number}","comments_url":"https://api.github.com/repos/cuberite/cuberite/issues/3151/comments","statuses_url":"https://api.github.com/repos/cuberite/cuberite/statuses/d1e8568ea3558d55fa76ce805ccc862ce8cdea25","head":{"label":"LogicParrot:smartChunkRam","ref":"smartChunkRam","sha":"d1e8568ea3558d55fa76ce805ccc862ce8cdea25","user":{"login":"LogicParrot","id":5030488,"avatar_url":"https://avatars.githubusercontent.com/u/5030488?v=3","gravatar_id":"","url":"https://api.github.com/users/LogicParrot","html_url":"https://github.com/LogicParrot","followers_url":"https://api.github.com/users/LogicParrot/followers","following_url":"https://api.github.com/users/LogicParrot/following{/other_user}","gists_url":"https://api.github.com/users/LogicParrot/gists{/gist_id}","starred_url":"https://api.github.com/users/LogicParrot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LogicParrot/subscriptions","organizations_url":"https://api.github.com/users/LogicParrot/orgs","repos_url":"https://api.github.com/users/LogicParrot/repos","events_url":"https://api.github.com/users/LogicParrot/events{/privacy}","received_events_url":"https://api.github.com/users/LogicParrot/received_events","type":"User","site_admin":false},"repo":{"id":43133123,"name":"cuberite","full_name":"LogicParrot/cuberite","owner":{"login":"LogicParrot","id":5030488,"avatar_url":"https://avatars.githubusercontent.com/u/5030488?v=3","gravatar_id":"","url":"https://api.github.com/users/LogicParrot","html_url":"https://github.com/LogicParrot","followers_url":"https://api.github.com/users/LogicParrot/followers","following_url":"https://api.github.com/users/LogicParrot/following{/other_user}","gists_url":"https://api.github.com/users/LogicParrot/gists{/gist_id}","starred_url":"https://api.github.com/users/LogicParrot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/LogicParrot/subscriptions","organizations_url":"https://api.github.com/users/LogicParrot/orgs","repos_url":"https://api.github.com/users/LogicParrot/repos","events_url":"https://api.github.com/users/LogicParrot/events{/privacy}","received_events_url":"https://api.github.com/users/LogicParrot/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/LogicParrot/cuberite","description":"A performant C++ Minecraft compatible game server","fork":true,"url":"https://api.github.com/repos/LogicParrot/cuberite","forks_url":"https://api.github.com/repos/LogicParrot/cuberite/forks","keys_url":"https://api.github.com/repos/LogicParrot/cuberite/keys{/key_id}","collaborators_url":"https://api.github.com/repos/LogicParrot/cuberite/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/LogicParrot/cuberite/teams","hooks_url":"https://api.github.com/repos/LogicParrot/cuberite/hooks","issue_events_url":"https://api.github.com/repos/LogicParrot/cuberite/issues/events{/number}","events_url":"https://api.github.com/repos/LogicParrot/cuberite/events","assignees_url":"https://api.github.com/repos/LogicParrot/cuberite/assignees{/user}","branches_url":"https://api.github.com/repos/LogicParrot/cuberite/branches{/branch}","tags_url":"https://api.github.com/repos/LogicParrot/cuberite/tags","blobs_url":"https://api.github.com/repos/LogicParrot/cuberite/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/LogicParrot/cuberite/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/LogicParrot/cuberite/git/refs{/sha}","trees_url":"https://api.github.com/repos/LogicParrot/cuberite/git/trees{/sha}","statuses_url":"https://api.github.com/repos/LogicParrot/cuberite/statuses/{sha}","languages_url":"https://api.github.com/repos/LogicParrot/cuberite/languages","stargazers_url":"https://api.github.com/repos/LogicParrot/cuberite/stargazers","contributors_url":"https://api.github.com/repos/LogicParrot/cuberite/contributors","subscribers_url":"https://api.github.com/repos/LogicParrot/cuberite/subscribers","subscription_url":"https://api.github.com/repos/LogicParrot/cuberite/subscription","commits_url":"https://api.github.com/repos/LogicParrot/cuberite/commits{/sha}","git_commits_url":"https://api.github.com/repos/LogicParrot/cuberite/git/commits{/sha}","comments_url":"https://api.github.com/repos/LogicParrot/cuberite/comments{/number}","issue_comment_url":"https://api.github.com/repos/LogicParrot/cuberite/issues/comments{/number}","contents_url":"https://api.github.com/repos/LogicParrot/cuberite/contents/{+path}","compare_url":"https://api.github.com/repos/LogicParrot/cuberite/compare/{base}...{head}","merges_url":"https://api.github.com/repos/LogicParrot/cuberite/merges","archive_url":"https://api.github.com/repos/LogicParrot/cuberite/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/LogicParrot/cuberite/downloads","issues_url":"https://api.github.com/repos/LogicParrot/cuberite/issues{/number}","pulls_url":"https://api.github.com/repos/LogicParrot/cuberite/pulls{/number}","milestones_url":"https://api.github.com/repos/LogicParrot/cuberite/milestones{/number}","notifications_url":"https://api.github.com/repos/LogicParrot/cuberite/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/LogicParrot/cuberite/labels{/name}","releases_url":"https://api.github.com/repos/LogicParrot/cuberite/releases{/id}","deployments_url":"https://api.github.com/repos/LogicParrot/cuberite/deployments","created_at":"2015-09-25T10:43:49Z","updated_at":"2016-01-11T17:22:23Z","pushed_at":"2016-05-04T05:59:26Z","git_url":"git://github.com/LogicParrot/cuberite.git","ssh_url":"git@github.com:LogicParrot/cuberite.git","clone_url":"https://github.com/LogicParrot/cuberite.git","svn_url":"https://github.com/LogicParrot/cuberite","homepage":"http://cuberite.org","size":76735,"stargazers_count":0,"watchers_count":0,"language":"C++","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"cuberite:master","ref":"master","sha":"b3d4e0fca665502b727f0088a3a20aac1b9ad073","user":{"login":"cuberite","id":8973961,"avatar_url":"https://avatars.githubusercontent.com/u/8973961?v=3","gravatar_id":"","url":"https://api.github.com/users/cuberite","html_url":"https://github.com/cuberite","followers_url":"https://api.github.com/users/cuberite/followers","following_url":"https://api.github.com/users/cuberite/following{/other_user}","gists_url":"https://api.github.com/users/cuberite/gists{/gist_id}","starred_url":"https://api.github.com/users/cuberite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cuberite/subscriptions","organizations_url":"https://api.github.com/users/cuberite/orgs","repos_url":"https://api.github.com/users/cuberite/repos","events_url":"https://api.github.com/users/cuberite/events{/privacy}","received_events_url":"https://api.github.com/users/cuberite/received_events","type":"Organization","site_admin":false},"repo":{"id":11693709,"name":"cuberite","full_name":"cuberite/cuberite","owner":{"login":"cuberite","id":8973961,"avatar_url":"https://avatars.githubusercontent.com/u/8973961?v=3","gravatar_id":"","url":"https://api.github.com/users/cuberite","html_url":"https://github.com/cuberite","followers_url":"https://api.github.com/users/cuberite/followers","following_url":"https://api.github.com/users/cuberite/following{/other_user}","gists_url":"https://api.github.com/users/cuberite/gists{/gist_id}","starred_url":"https://api.github.com/users/cuberite/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cuberite/subscriptions","organizations_url":"https://api.github.com/users/cuberite/orgs","repos_url":"https://api.github.com/users/cuberite/repos","events_url":"https://api.github.com/users/cuberite/events{/privacy}","received_events_url":"https://api.github.com/users/cuberite/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/cuberite/cuberite","description":"A custom Minecraft compatible game server written in C++","fork":false,"url":"https://api.github.com/repos/cuberite/cuberite","forks_url":"https://api.github.com/repos/cuberite/cuberite/forks","keys_url":"https://api.github.com/repos/cuberite/cuberite/keys{/key_id}","collaborators_url":"https://api.github.com/repos/cuberite/cuberite/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/cuberite/cuberite/teams","hooks_url":"https://api.github.com/repos/cuberite/cuberite/hooks","issue_events_url":"https://api.github.com/repos/cuberite/cuberite/issues/events{/number}","events_url":"https://api.github.com/repos/cuberite/cuberite/events","assignees_url":"https://api.github.com/repos/cuberite/cuberite/assignees{/user}","branches_url":"https://api.github.com/repos/cuberite/cuberite/branches{/branch}","tags_url":"https://api.github.com/repos/cuberite/cuberite/tags","blobs_url":"https://api.github.com/repos/cuberite/cuberite/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/cuberite/cuberite/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/cuberite/cuberite/git/refs{/sha}","trees_url":"https://api.github.com/repos/cuberite/cuberite/git/trees{/sha}","statuses_url":"https://api.github.com/repos/cuberite/cuberite/statuses/{sha}","languages_url":"https://api.github.com/repos/cuberite/cuberite/languages","stargazers_url":"https://api.github.com/repos/cuberite/cuberite/stargazers","contributors_url":"https://api.github.com/repos/cuberite/cuberite/contributors","subscribers_url":"https://api.github.com/repos/cuberite/cuberite/subscribers","subscription_url":"https://api.github.com/repos/cuberite/cuberite/subscription","commits_url":"https://api.github.com/repos/cuberite/cuberite/commits{/sha}","git_commits_url":"https://api.github.com/repos/cuberite/cuberite/git/commits{/sha}","comments_url":"https://api.github.com/repos/cuberite/cuberite/comments{/number}","issue_comment_url":"https://api.github.com/repos/cuberite/cuberite/issues/comments{/number}","contents_url":"https://api.github.com/repos/cuberite/cuberite/contents/{+path}","compare_url":"https://api.github.com/repos/cuberite/cuberite/compare/{base}...{head}","merges_url":"https://api.github.com/repos/cuberite/cuberite/merges","archive_url":"https://api.github.com/repos/cuberite/cuberite/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/cuberite/cuberite/downloads","issues_url":"https://api.github.com/repos/cuberite/cuberite/issues{/number}","pulls_url":"https://api.github.com/repos/cuberite/cuberite/pulls{/number}","milestones_url":"https://api.github.com/repos/cuberite/cuberite/milestones{/number}","notifications_url":"https://api.github.com/repos/cuberite/cuberite/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/cuberite/cuberite/labels{/name}","releases_url":"https://api.github.com/repos/cuberite/cuberite/releases{/id}","deployments_url":"https://api.github.com/repos/cuberite/cuberite/deployments","created_at":"2013-07-26T19:56:59Z","updated_at":"2016-05-12T23:17:07Z","pushed_at":"2016-05-12T15:57:39Z","git_url":"git://github.com/cuberite/cuberite.git","ssh_url":"git@github.com:cuberite/cuberite.git","clone_url":"https://github.com/cuberite/cuberite.git","svn_url":"https://github.com/cuberite/cuberite","homepage":"http://cuberite.org","size":160325,"stargazers_count":1289,"watchers_count":1289,"language":"C++","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":291,"mirror_url":null,"open_issues_count":502,"forks":291,"open_issues":502,"watchers":1289,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/cuberite/cuberite/pulls/3151"},"html":{"href":"https://github.com/cuberite/cuberite/pull/3151"},"issue":{"href":"https://api.github.com/repos/cuberite/cuberite/issues/3151"},"comments":{"href":"https://api.github.com/repos/cuberite/cuberite/issues/3151/comments"},"review_comments":{"href":"https://api.github.com/repos/cuberite/cuberite/pulls/3151/comments"},"review_comment":{"href":"https://api.github.com/repos/cuberite/cuberite/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/cuberite/cuberite/pulls/3151/commits"},"statuses":{"href":"https://api.github.com/repos/cuberite/cuberite/statuses/d1e8568ea3558d55fa76ce805ccc862ce8cdea25"}}}}