Win32_QFork.cpp 49.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (c), Microsoft Open Technologies, Inc.
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

23 24 25 26 27 28 29 30 31
#include <Windows.h>
#include <errno.h>
#include <stdio.h>
#include <wchar.h>
#include <Psapi.h>

#define QFORK_MAIN_IMPL
#include "Win32_QFork.h"

32
#include "Win32_QFork_impl.h"
33 34
#include "Win32_dlmalloc.h"
#include "Win32_SmartHandle.h"
35
#include "Win32_Service.h"
36 37
#include <vector>
#include <iostream>
38 39
#include <fstream>
#include <sstream>
40
#include <stdint.h>
41
#include <exception>
42 43
using namespace std;

44 45 46
const long long cSentinelHeapSize = 30 * 1024 * 1024;
extern "C" int checkForSentinelMode(int argc, char **argv);

47 48 49 50 51 52
extern "C"
{
	// forward def from util.h. 
	long long memtoll(const char *p, int *err);
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
//#define DEBUG_WITH_PROCMON
#ifdef DEBUG_WITH_PROCMON
#define FILE_DEVICE_PROCMON_LOG 0x00009535
#define IOCTL_EXTERNAL_LOG_DEBUGOUT (ULONG) CTL_CODE( FILE_DEVICE_PROCMON_LOG, 0x81, METHOD_BUFFERED, FILE_WRITE_ACCESS )

HANDLE hProcMonDevice = INVALID_HANDLE_VALUE;
BOOL WriteToProcmon (wstring message)
{
    if (hProcMonDevice != INVALID_HANDLE_VALUE) {
        DWORD nb = 0;
        return DeviceIoControl(
            hProcMonDevice, 
            IOCTL_EXTERNAL_LOG_DEBUGOUT,
            (LPVOID)(message.c_str()),
            (DWORD)(message.length() * sizeof(wchar_t)),
            NULL,
            0,
            &nb,
            NULL);
    } else {
        return FALSE;
    }
}
#endif

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
/*
Redis is an in memory DB. We need to share the redis database with a quasi-forked process so that we can do the RDB and AOF saves 
without halting the main redis process, or crashing due to code that was never designed to be thread safe. Essentially we need to
replicate the COW behavior of fork() on Windows, but we don't actually need a complete fork() implementation. A complete fork() 
implementation would require subsystem level support to make happen. The following is required to make this quasi-fork scheme work:

DLMalloc (http://g.oswego.edu/dl/html/malloc.html):
    - replaces malloc/realloc/free, either by manual patching of the zmalloc code in Redis or by patching the CRT routines at link time
    - partitions space into segments that it allocates from (currently configured as 64MB chunks)
    - we map/unmap these chunks as requested into a memory map (unmapping allows the system to decide how to reduce the physical memory 
      pressure on system)

DLMallocMemoryMap:
   - An uncomitted memory map whose size is the total physical memory on the system less some memory for the rest of the system so that 
     we avoid excessive swapping.
   - This is reserved high in VM space so that it can be mapped at a specific address in the child qforked process (ASLR must be 
     disabled for these processes)
   - This must be mapped in exactly the same virtual memory space in both forker and forkee.

QForkConrolMemoryMap:
   - contains a map of the allocated segments in the DLMallocMemoryMap
   - contains handles for inter-process synchronization
   - contains pointers to some of the global data in the parent process if mapped into DLMallocMemoryMap, and a copy of any other 
     required global data

QFork process:
    - a copy of the parent process with a command line specifying QFork behavior
    - when a COW operation is requested via an event signal
        - opens the DLMAllocMemoryMap with PAGE_WRITECOPY
        - reserve space for DLMAllocMemoryMap at the memory location specified in ControlMemoryMap
        - locks the DLMalloc segments as specified in QForkConrolMemoryMap 
        - maps global data from the QForkConrolMEmoryMap into this process
        - executes the requested operation
        - unmaps all the mm views (discarding any writes)
        - signals the parent when the operation is complete

How the parent invokes the QFork process:
    - protects mapped memory segments with VirtualProtect using PAGE_WRITECOPY (both the allocated portions of DLMAllocMemoryMap and 
      the QForkConrolMemoryMap)
    - QForked process is signaled to process command
    - Parent waits (asynchronously) until QForked process signals that operation is complete, then as an atomic operation:
        - signals and waits for the forked process to terminate
        - resotres protection status on mapped blocks
        - determines which pages have been modified and copies these to a buffer
        - unmaps the view of the heap (discarding COW changes form the view)
        - remaps the view
        - copies the changes back into the view
*/

#ifndef LODWORD
    #define LODWORD(_qw)    ((DWORD)(_qw))
#endif
#ifndef HIDWORD
    #define HIDWORD(_qw)    ((DWORD)(((_qw) >> (sizeof(DWORD)*8)) & DWORD(~0)))
#endif

134 135
const SIZE_T cAllocationGranularity = 1 << 18;                    // 256KB per heap block (matches large block allocation threshold of dlmalloc)
const int cMaxBlocks = 1 << 24;                                   // 256KB * 16M heap blocks = 4TB. 4TB is the largest memory config Windows supports at present.
136 137
const wchar_t* cMapFileBaseName = L"RedisQFork";
const char* qforkFlag = "--QFork";
138
const char* maxmemoryFlag = "maxmemory";
139
const char* maxheapFlag = "maxheap";
140
const char* includeFlag = "include";
141
const int cDeadForkWait = 30000;
142
size_t pageSize = 0;
143

144
enum class BlockState : std::uint8_t {bsINVALID = 0, bsUNMAPPED = 1, bsMAPPED = 2};
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

struct QForkControl {
    HANDLE heapMemoryMapFile;
    HANDLE heapMemoryMap;
    int availableBlocksInHeap;                 // number of blocks in blockMap (dynamically determined at run time)
    SIZE_T heapBlockSize;           
    BlockState heapBlockMap[cMaxBlocks];
    LPVOID heapStart;

    OperationType typeOfOperation;
    HANDLE forkedProcessReady;
    HANDLE startOperation;
    HANDLE operationComplete;
    HANDLE operationFailed;
    HANDLE terminateForkedProcess;

    // global data pointers to be passed to the forked process
    QForkBeginInfo globalData;
    BYTE DLMallocGlobalState[1000];
    size_t DLMallocGlobalStateSize;
};

QForkControl* g_pQForkControl;
HANDLE g_hQForkControlFileMap;
HANDLE g_hForkedProcess;
170
DWORD g_systemAllocationGranularity;
171
int g_SlaveExitCode = 0; // For slave process
172 173 174

BOOL QForkSlaveInit(HANDLE QForkConrolMemoryMapHandle, DWORD ParentProcessID) {
    try {
175
		SmartHandle shParent( 
176 177 178 179 180 181 182
            OpenProcess(SYNCHRONIZE | PROCESS_DUP_HANDLE, TRUE, ParentProcessID),
            string("Could not open parent process"));

        SmartHandle shMMFile(shParent, QForkConrolMemoryMapHandle);
        SmartFileView<QForkControl> sfvMasterQForkControl( 
            shMMFile, 
            FILE_MAP_COPY, 
J
Jonathan Pickett 已提交
183
            string("Could not map view of QForkControl in slave. Is system swap file large enough?"));
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
        g_pQForkControl = sfvMasterQForkControl;

        // duplicate handles and stuff into control structure (master protected by PAGE_WRITECOPY)
        SmartHandle dupHeapFileHandle(shParent, sfvMasterQForkControl->heapMemoryMapFile);
        g_pQForkControl->heapMemoryMapFile = dupHeapFileHandle;
        SmartHandle dupForkedProcessReady(shParent,sfvMasterQForkControl->forkedProcessReady);
        g_pQForkControl->forkedProcessReady = dupForkedProcessReady;
        SmartHandle dupStartOperation(shParent,sfvMasterQForkControl->startOperation);
        g_pQForkControl->startOperation = dupStartOperation;
        SmartHandle dupOperationComplete(shParent,sfvMasterQForkControl->operationComplete);
        g_pQForkControl->operationComplete = dupOperationComplete;
        SmartHandle dupOperationFailed(shParent,sfvMasterQForkControl->operationFailed);
        g_pQForkControl->operationFailed = dupOperationFailed;
        SmartHandle dupTerminateProcess(shParent,sfvMasterQForkControl->terminateForkedProcess);
        g_pQForkControl->terminateForkedProcess = dupTerminateProcess;

       // create section handle on MM file
       SIZE_T mmSize = g_pQForkControl->availableBlocksInHeap * cAllocationGranularity;
       SmartFileMapHandle sfmhMapFile(
           g_pQForkControl->heapMemoryMapFile, 
           PAGE_WRITECOPY, 
           HIDWORD(mmSize), LODWORD(mmSize),
206
           string("Could not open file mapping object in slave"));
207 208
       g_pQForkControl->heapMemoryMap = sfmhMapFile;

209 210 211

	   // The key to mapping a heap larger than physical memory is to not map it all at once. 
	   SmartFileView<byte> sfvHeap(
212 213
            g_pQForkControl->heapMemoryMap,
            FILE_MAP_COPY,
214 215
            0, 0, 
			cAllocationGranularity,	// Only map a portion of the heap . Deal with the unmapped pages with a VEH.
216
            g_pQForkControl->heapStart,
217
            string("Could not map heap in forked process. Is system swap file large enough?"));
218 219 220

        // setup DLMalloc global data
        if( SetDLMallocGlobalState(g_pQForkControl->DLMallocGlobalStateSize, g_pQForkControl->DLMallocGlobalState) != 0) {
221
            throw std::runtime_error("DLMalloc global state copy failed.");
222 223 224 225 226 227 228 229
        }

        // signal parent that we are ready
        SetEvent(g_pQForkControl->forkedProcessReady);

        // wait for parent to signal operation start
        WaitForSingleObject(g_pQForkControl->startOperation, INFINITE);

230
        // copy redis globals into fork process
231
        SetupGlobals(g_pQForkControl->globalData.globalData, g_pQForkControl->globalData.globalDataSize, g_pQForkControl->globalData.dictHashSeed);
232

233
        // execute requested operation
234
        if (g_pQForkControl->typeOfOperation == OperationType::otRDB) {
235
			g_SlaveExitCode = do_rdbSave(g_pQForkControl->globalData.filename);
236
        } else if (g_pQForkControl->typeOfOperation == OperationType::otAOF) {
237
			g_SlaveExitCode = do_aofSave(g_pQForkControl->globalData.filename);
238
        } else {
239
            throw runtime_error("unexpected operation type");
240 241
        }

242
        // let parent know we are done
243 244 245 246 247 248 249 250 251
        SetEvent(g_pQForkControl->operationComplete);

        // parent will notify us when to quit
        WaitForSingleObject(g_pQForkControl->terminateForkedProcess, INFINITE);

        g_pQForkControl = NULL;
        return TRUE;
    }
    catch(std::system_error syserr) {
252
        printf("QForkSlaveInit: system error caught. error code=0x%08x, message=%s\n", syserr.code().value(), syserr.what());
253 254 255 256 257 258 259 260 261
        g_pQForkControl = NULL;
        if(g_pQForkControl != NULL) {
            if(g_pQForkControl->operationFailed != NULL) {
                SetEvent(g_pQForkControl->operationFailed);
            }
        }
        return FALSE;
    }
    catch(std::runtime_error runerr) {
262
        printf("QForkSlaveInit: runtime error caught. message=%s\n", runerr.what());
263 264 265 266
        g_pQForkControl = NULL;
        SetEvent(g_pQForkControl->operationFailed);
        return FALSE;
    }
267
    return FALSE;
268 269
}

270
BOOL QForkMasterInit( __int64 maxheapBytes ) {
271 272 273 274 275 276 277 278 279 280 281 282
    try {
        // allocate file map for qfork control so it can be passed to the forked process
        g_hQForkControlFileMap = CreateFileMappingW(
            INVALID_HANDLE_VALUE,
            NULL,
            PAGE_READWRITE,
            0, sizeof(QForkControl),
            NULL);
        if (g_hQForkControlFileMap == NULL) {
            throw std::system_error(
                GetLastError(),
                system_category(),
283
                "CreateFileMapping failed");
284
        }
285

286 287 288 289 290 291 292 293 294
        g_pQForkControl = (QForkControl*)MapViewOfFile(
            g_hQForkControlFileMap, 
            FILE_MAP_ALL_ACCESS,
            0, 0,
            0);
        if (g_pQForkControl == NULL) {
            throw std::system_error(
                GetLastError(),
                system_category(),
295
                "MapViewOfFile failed");
296
        }
297

298 299 300 301 302 303
        // This must be called only once per process! Calling it more times than that will not recreate existing 
        // section, and dlmalloc will ultimately fail with an access violation. Once is good.
        if (dlmallopt(M_GRANULARITY, cAllocationGranularity) == 0) {
            throw std::system_error(
                GetLastError(),
                system_category(),
304
                "DLMalloc failed initializing allocation granularity.");
305 306 307
        }
        g_pQForkControl->heapBlockSize = cAllocationGranularity;

308 309 310 311 312
		// ensure the number of blocks is a multiple of cAllocationGranularity
		SIZE_T allocationBlocks = maxheapBytes / cAllocationGranularity;
		allocationBlocks += ((maxheapBytes % cAllocationGranularity) != 0);

        g_pQForkControl->availableBlocksInHeap = (int)allocationBlocks;
313 314
        if (g_pQForkControl->availableBlocksInHeap <= 0) {
            throw std::runtime_error(
315
                "Invalid number of heap blocks.");
316
        }
317

318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
        wchar_t heapMemoryMapPath[MAX_PATH];
        swprintf_s( 
            heapMemoryMapPath, 
            MAX_PATH, 
            L"%s_%d.dat", 
            cMapFileBaseName, 
            GetCurrentProcessId());

        g_pQForkControl->heapMemoryMapFile = 
            CreateFileW( 
                heapMemoryMapPath,
                GENERIC_READ | GENERIC_WRITE,
                0,
                NULL,
                CREATE_ALWAYS,
                FILE_ATTRIBUTE_NORMAL| FILE_FLAG_DELETE_ON_CLOSE,
                NULL );
        if (g_pQForkControl->heapMemoryMapFile == INVALID_HANDLE_VALUE) {
            throw std::system_error(
                GetLastError(),
                system_category(),
339
                "CreateFileW failed.");
340
        }
341

342 343 344 345 346 347 348
		// There is a strange random failure toawrds the end of mapping the heap in the forked process in the VEH if
		// the underlying MMF is not larger than the MM space we are using. This seems to be some sort of 
		// memory->file allocation granularity issue. Increasing the size of the file (by 16MB) takes care of the 
		// issue in all cases.
		const size_t extraMMF = 64 * cAllocationGranularity;

		SIZE_T mmSize = g_pQForkControl->availableBlocksInHeap * cAllocationGranularity + extraMMF; 
349 350 351 352 353 354 355 356 357 358 359 360
        g_pQForkControl->heapMemoryMap = 
            CreateFileMappingW( 
                g_pQForkControl->heapMemoryMapFile,
                NULL,
                PAGE_READWRITE,
                HIDWORD(mmSize),
                LODWORD(mmSize),
                NULL);
        if (g_pQForkControl->heapMemoryMap == NULL) {
            throw std::system_error(
                GetLastError(),
                system_category(),
361
                "CreateFileMapping failed.");
362
        }
363
            
364 365 366 367 368 369 370 371 372 373 374 375 376
        // Find a place in the virtual memory space where we can reserve space for our allocations that is likely
        // to be available in the forked process.  (If this ever fails in the forked process, we will have to launch
        // the forked process and negotiate for a shared memory address here.)
        LPVOID pHigh = VirtualAllocEx( 
            GetCurrentProcess(),
            NULL,
            mmSize,
            MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, 
            PAGE_READWRITE);
        if (pHigh == NULL) {
            throw std::system_error(
                GetLastError(),
                system_category(),
377
                "VirtualAllocEx failed.");
378 379 380 381 382
        }
        if (VirtualFree(pHigh, 0, MEM_RELEASE) == FALSE) {
            throw std::system_error(
                GetLastError(),
                system_category(),
383
                "VirtualFree failed.");
384
        }
385

386 387 388 389 390 391 392 393 394 395 396
        g_pQForkControl->heapStart = 
            MapViewOfFileEx(
                g_pQForkControl->heapMemoryMap,
                FILE_MAP_ALL_ACCESS,
                0,0,                            
                0,  
                pHigh);
        if (g_pQForkControl->heapStart == NULL) {
            throw std::system_error(
                GetLastError(),
                system_category(),
397
                "MapViewOfFileEx failed.");
398
        }
399

400 401 402 403 404
        for (int n = 0; n < cMaxBlocks; n++) {
            g_pQForkControl->heapBlockMap[n] = 
                ((n < g_pQForkControl->availableBlocksInHeap) ?
                BlockState::bsUNMAPPED : BlockState::bsINVALID);
        }
405

406 407 408 409 410 411
        g_pQForkControl->typeOfOperation = OperationType::otINVALID;
        g_pQForkControl->forkedProcessReady = CreateEvent(NULL,TRUE,FALSE,NULL);
        if (g_pQForkControl->forkedProcessReady == NULL) {
            throw std::system_error(
                GetLastError(),
                system_category(),
412
                "CreateEvent failed.");
413 414 415 416 417 418
        }
        g_pQForkControl->startOperation = CreateEvent(NULL,TRUE,FALSE,NULL);
        if (g_pQForkControl->startOperation == NULL) {
            throw std::system_error(
                GetLastError(),
                system_category(),
419
                "CreateEvent failed.");
420 421 422 423 424 425
        }
        g_pQForkControl->operationComplete = CreateEvent(NULL,TRUE,FALSE,NULL);
        if (g_pQForkControl->operationComplete == NULL) {
            throw std::system_error(
                GetLastError(),
                system_category(),
426
                "CreateEvent failed.");
427 428 429 430 431 432
        }
        g_pQForkControl->operationFailed = CreateEvent(NULL,TRUE,FALSE,NULL);
        if (g_pQForkControl->operationFailed == NULL) {
            throw std::system_error(
                GetLastError(),
                system_category(),
433
                "CreateEvent failed.");
434 435 436 437 438 439
        }
        g_pQForkControl->terminateForkedProcess = CreateEvent(NULL,TRUE,FALSE,NULL);
        if (g_pQForkControl->terminateForkedProcess == NULL) {
            throw std::system_error(
                GetLastError(),
                system_category(),
440
                "CreateEvent failed.");
441 442 443
        }

        return TRUE;
444
    }
445 446
    catch(std::system_error syserr) {
        printf("QForkMasterInit: system error caught. error code=0x%08x, message=%s\n", syserr.code().value(), syserr.what());
447
    }
448 449
    catch(std::runtime_error runerr) {
        printf("QForkMasterInit: runtime error caught. message=%s\n", runerr.what());
450
    }
451 452
    catch(...) {
        printf("QForkMasterInit: other exception caught.\n");
453
    }
454 455 456
    return FALSE;
}

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
LONG CALLBACK VectoredHeapMapper(PEXCEPTION_POINTERS info) {
	if( info->ExceptionRecord->ExceptionCode == STATUS_ACCESS_VIOLATION && 
		info->ExceptionRecord->NumberParameters == 2) {
		intptr_t failingMemoryAddress = info->ExceptionRecord->ExceptionInformation[1];
		intptr_t heapStart = (intptr_t)g_pQForkControl->heapStart;
		intptr_t heapEnd = heapStart + ((SIZE_T)g_pQForkControl->availableBlocksInHeap * g_pQForkControl->heapBlockSize);
		if( failingMemoryAddress >= heapStart && failingMemoryAddress <= heapEnd )
		{
			intptr_t startOfMapping = failingMemoryAddress - failingMemoryAddress % g_systemAllocationGranularity;
			intptr_t mmfOffset = startOfMapping - heapStart;
			size_t bytesToMap = min( g_systemAllocationGranularity, heapEnd - startOfMapping);
			LPVOID pMapped =  MapViewOfFileEx( 
				g_pQForkControl->heapMemoryMap, 
				FILE_MAP_COPY,
				HIDWORD(mmfOffset),
				LODWORD(mmfOffset),
				bytesToMap,
				(LPVOID)startOfMapping);
			if(pMapped != NULL)
			{
				return EXCEPTION_CONTINUE_EXECUTION;
			}
			else
			{
				printf("\nF(0x%p)", startOfMapping);
				printf( "\t MapViewOfFileEx failed with error 0x%08X. \n", GetLastError() );
				printf( "\t heapStart 0x%p\n", heapStart);
				printf( "\t heapEnd 0x%p\n", heapEnd);
				printf( "\t failing access location 0x%p\n", failingMemoryAddress);
				printf( "\t offset into mmf to start mapping 0x%016X\n", mmfOffset);
				printf( "\t start of new mapping 0x%p \n", startOfMapping);
				printf( "\t bytes to map 0x%08x \n", bytesToMap);
				printf( "\t continuing exception handler search \n" );
			}
		}
	}

	return EXCEPTION_CONTINUE_SEARCH;
}

497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
/*
    Returns true if we have successfully parsed the conf file and its recursive includes. 
    If maxheap and/or maxmemory is specified these will be set on exit. If maxheap/maxmemory 
    are specified in master and/or recursive conf files, the first encountered flag is taken.
*/
bool ParseConfFile(string file, __int64& maxheapBytes, __int64& maxmemoryBytes) {
    int memtollerr = 0;
    ifstream config;
    config.open(file);
    if (config.fail()) {
        return false;
    }

    while (!config.eof()) {
        string line;
        getline(config, line);
        istringstream iss(line);
        string token;
        if (getline(iss, token, ' ')) {
            if (_stricmp(token.c_str(), maxmemoryFlag) == 0) {
                string maxmemoryString;
                if (getline(iss, maxmemoryString, ' ')) {
                    if (maxmemoryBytes == -1) {
                        maxmemoryBytes = memtoll(maxmemoryString.c_str(), &memtollerr);
                        if (memtollerr != 0) {
                            printf(
                                "Unable to convert %s to the number of bytes for the maxmemory flag.\n",
                                maxmemoryString.c_str());
                            printf("Failing startup.\n");
                            return false;
                        }
                    }
                }
            } else if (_stricmp(token.c_str(), maxheapFlag) == 0) {
                string maxheapString;
                if (getline(iss, maxheapString, ' ')) {
                    if (maxheapBytes == -1) {
                        maxheapBytes = memtoll(maxheapString.c_str(), &memtollerr);
                        if (memtollerr != 0) {
                            printf(
                                "Unable to convert %s to the number of bytes for the maxmemory flag.\n",
                                maxheapString.c_str());
                            printf("Failing startup.\n");
                            return false;
                        }
                    }
                }
            } else if (_stricmp(token.c_str(), includeFlag) == 0) {
                string includeFile;
                if (getline(iss, includeFile, ' ')) {
                    if (!ParseConfFile(includeFile, maxheapBytes, maxmemoryBytes)) {
                        return false;
                    }
                }
            }
        }
    }

    return true;
}

558 559 560 561 562 563

// QFork API
StartupStatus QForkStartup(int argc, char** argv) {
    bool foundSlaveFlag = false;
    HANDLE QForkConrolMemoryMapHandle = NULL;
    DWORD PPID = 0;
564 565
    __int64 maxheapBytes = -1;
    __int64 maxmemoryBytes = -1;
566
    int memtollerr = 0;
567 568 569 570 571

	SYSTEM_INFO si;
    GetSystemInfo(&si);
    g_systemAllocationGranularity = si.dwAllocationGranularity;

572 573 574 575 576 577 578
    if ((argc == 3) && (strcmp(argv[0], qforkFlag) == 0)) {
        // slave command line looks like: --QFork [QForkConrolMemoryMap handle] [parent process id]
        foundSlaveFlag = true;
        char* endPtr;
        QForkConrolMemoryMapHandle = (HANDLE)strtoul(argv[1],&endPtr,10);
        char* end = NULL;
        PPID = strtoul(argv[2], &end, 10);
579
    } else {
580
        for (int n = 1; n < argc; n++) {
581
			// check for flags in .conf file
582
			if( n == 1  && strncmp(argv[n],"--",2) != 0 ) {
583 584 585 586 587
                if (!ParseConfFile(argv[1], maxheapBytes, maxmemoryBytes)) {
                    return StartupStatus::ssFAILED;
                } else {
                    continue;
                }
588
			}
589 590
            if( strncmp(argv[n],"--", 2) == 0) {
				if (_stricmp(argv[n]+2,maxmemoryFlag) == 0) {
591 592
					maxmemoryBytes = memtoll(argv[n+1],&memtollerr);
					if( memtollerr != 0) {
593
						printf (
594 595
							"%s specified. Unable to convert %s to the number of bytes for the maxmemory flag.\n", 
							maxmemoryBytes,
596 597 598
							argv[n+1] );
						printf( "Failing startup.\n");
						return StartupStatus::ssFAILED;
599 600
					}
				} else if(_stricmp(argv[n]+2,maxheapFlag) == 0) {
601 602
					maxheapBytes = memtoll(argv[n+1],&memtollerr);
					if( memtollerr != 0) {
603
						printf (
604 605
							"%s specified. Unable to convert %s to the number of bytes for the maxmemory flag.\n", 
							maxmemoryBytes,
606 607 608 609
							argv[n+1] );
						printf( "Failing startup.\n");
						return StartupStatus::ssFAILED;
					}
610 611
				}
			}
612
        }
613 614
    }

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
	PERFORMANCE_INFORMATION perfinfo;
	perfinfo.cb = sizeof(PERFORMANCE_INFORMATION);
	if (FALSE == GetPerformanceInfo(&perfinfo, sizeof(PERFORMANCE_INFORMATION))) {
		printf ( "GetPerformanceInfo failed.\n" ); 
		printf( "Failing startup.\n" );
		return StartupStatus::ssFAILED;
	}
	pageSize = perfinfo.PageSize;

	/*
	Not specifying the maxmemory or maxheap flags will result in the default behavior of: new key generation not 
	bounded by heap usage, and the heap size equal to the size of physical memory.

	Redis will respect the maxmemory flag by preventing new key creation when the number of bytes allocated in the heap 
	exceeds the level specified by the maxmemory flag. This does not account for heap fragmentation or memory usage by 
	the heap allocator. To allow for this extra space maxheapBytes is implicitly set to (1.5 * maxmemory [rounded up 
	to the nearest cAllocationGranularity boundary]). The maxheap flag may be specified along with the maxmemory flag to
	increase the heap further than this. 

	If the maxmemory flag is not specified, but the maxheap flag is specified, the heap is sized according to this flag
	(rounded up to the nearest cAllocationGranularity boundary). The heap may be configured larger than physical memory with
	this flag. If maxmemory is sufficiently large enough, the heap will also be made larger than physical memory. This 
	has implications for the system swap file size requirement and disk usage as discussed below. Specifying a heap larger
	than physical memory allows Redis to continue operating into virtual memory up to the limit of the heap size specified.

	Since the heap is entirely contained in the memory mapped file we are creating to share with the forked process, the
	size of the memory mapped file will be equal to the size of the heap. There must be sufficient disk space for this file.
	For instance, launching Redis on a server machine with 512GB of RAM and no flags specified for either maxmemory or 
	maxheap will result in the allocation of a 512GB memory mapped file. Redis will fail to launch if there is not enough 
	space available on the disk where redis is being launched from for this file.

	During forking the system swap file will be used for managing virtual memory sharing and the copy on write pages for both 
	forker and forkee. There must be sufficient swap space availability for this. The maximum size of this swap space commit
	is roughly equal to (physical memory + (2 * size of the memory allocated in the redis heap)). For instance, if the heap is nearly 
	maxed out on an 8GB machine and the heap has been configured to be twice the size of physical memory, the swap file comittment 
	will be (physical + (2 * (2 * physical)) or (5 * physical). By default Windows will dynamically allocate a swap file that will
	expand up to about (3.5 * physical). In this case the forked process will fail with ERROR_COMMITMENT_LIMIT (1455/0x5AF) error.  
	The fix for this is to ensure the system swap space is sufficiently large enough to handle this. The reason that the default 
	heap size is equal to physical memory is so that Redis will work on a freshly configured OS without requireing reconfiguring  
	either Redis or the machine (max comittment of (3 * physical)).
	*/
	int64_t maxMemoryPlusHalf = (3 * maxmemoryBytes) / 2;
	if( maxmemoryBytes != -1 ) {
		maxheapBytes = (maxheapBytes > maxMemoryPlusHalf) ? maxheapBytes : maxMemoryPlusHalf;
	}
	if( maxheapBytes == -1 )
	{
662 663 664 665 666 667
        if (checkForSentinelMode(argc, argv)) {
            // Sentinel mode does not need a large heap. This conserves disk space and page file reservation requirements.
            maxheapBytes = cSentinelHeapSize;
        } else {
            maxheapBytes = perfinfo.PhysicalTotal * pageSize;
        }
668 669
	}

670
    if (foundSlaveFlag) {
671 672 673 674 675 676 677
		LPVOID exceptionHandler = AddVectoredExceptionHandler( 1, VectoredHeapMapper );
		StartupStatus retVal = StartupStatus::ssFAILED;
		try {
			retVal = QForkSlaveInit( QForkConrolMemoryMapHandle, PPID ) ? StartupStatus::ssSLAVE_EXIT : StartupStatus::ssFAILED;
		} catch (...) { }
		RemoveVectoredExceptionHandler(exceptionHandler);		
		return retVal;
678
    } else {
679
		return QForkMasterInit(maxheapBytes) ? StartupStatus::ssCONTINUE_AS_MASTER : StartupStatus::ssFAILED;
680 681 682 683 684 685
    }
}

BOOL QForkShutdown() {
    if(g_hForkedProcess != NULL) {
        TerminateProcess(g_hForkedProcess, -1);
686
		CloseHandle(g_hForkedProcess);
687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
        g_hForkedProcess = NULL;
    }

    if( g_pQForkControl != NULL )
    {
        if (g_pQForkControl->forkedProcessReady != NULL) {
            CloseHandle(g_pQForkControl->forkedProcessReady);
            g_pQForkControl->forkedProcessReady = NULL;
        }
        if (g_pQForkControl->startOperation != NULL) {
            CloseHandle(g_pQForkControl->startOperation);
            g_pQForkControl->startOperation = NULL;
        }
        if (g_pQForkControl->operationComplete != NULL) {
            CloseHandle(g_pQForkControl->operationComplete);
            g_pQForkControl->operationComplete = NULL;
        }
        if (g_pQForkControl->operationFailed != NULL) {
            CloseHandle(g_pQForkControl->operationFailed);
            g_pQForkControl->operationFailed = NULL;
        }
        if (g_pQForkControl->terminateForkedProcess != NULL) {
            CloseHandle(g_pQForkControl->terminateForkedProcess);
            g_pQForkControl->terminateForkedProcess = NULL;
        }
        if (g_pQForkControl->heapMemoryMap != NULL) {
            CloseHandle(g_pQForkControl->heapMemoryMap);
            g_pQForkControl->heapMemoryMap = NULL;
        }
        if (g_pQForkControl->heapMemoryMapFile != INVALID_HANDLE_VALUE) {
            CloseHandle(g_pQForkControl->heapMemoryMapFile);
            g_pQForkControl->heapMemoryMapFile = INVALID_HANDLE_VALUE;
        }
        if (g_pQForkControl->heapStart != NULL) {
            UnmapViewOfFile(g_pQForkControl->heapStart);
            g_pQForkControl->heapStart = NULL;
        }

        if(g_pQForkControl != NULL) {
            UnmapViewOfFile(g_pQForkControl);
            g_pQForkControl = NULL;
        }
        if (g_hQForkControlFileMap != NULL) {
            CloseHandle(g_hQForkControlFileMap);
            g_hQForkControlFileMap = NULL;
        };
    }

    return TRUE;
}

738
BOOL BeginForkOperation(OperationType type, char* fileName, LPVOID globalData, int sizeOfGlobalData, DWORD* childPID, uint32_t dictHashSeed) {
739 740 741 742 743
    try {
        // copy operation data
        g_pQForkControl->typeOfOperation = type;
        strcpy_s(g_pQForkControl->globalData.filename, fileName);
        if (sizeOfGlobalData > MAX_GLOBAL_DATA) {
744
            throw std::runtime_error("Global state too large.");
745 746 747
        }
        memcpy(&(g_pQForkControl->globalData.globalData), globalData, sizeOfGlobalData);
        g_pQForkControl->globalData.globalDataSize = sizeOfGlobalData;
748
        g_pQForkControl->globalData.dictHashSeed = dictHashSeed;
749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

        GetDLMallocGlobalState(&g_pQForkControl->DLMallocGlobalStateSize, NULL);
        if (g_pQForkControl->DLMallocGlobalStateSize > sizeof(g_pQForkControl->DLMallocGlobalState)) {
            throw std::runtime_error("DLMalloc global state too large.");
        }
        if(GetDLMallocGlobalState(&g_pQForkControl->DLMallocGlobalStateSize, g_pQForkControl->DLMallocGlobalState) != 0) {
            throw std::runtime_error("DLMalloc global state copy failed.");
        }

        // protect both the heap and the fork control map from propagating local changes 
        DWORD oldProtect = 0;
        if (VirtualProtect(g_pQForkControl, sizeof(QForkControl), PAGE_WRITECOPY, &oldProtect) == FALSE) {
            throw std::system_error(
                GetLastError(),
                system_category(),
                "BeginForkOperation: VirtualProtect failed");
        }
        if (VirtualProtect( 
            g_pQForkControl->heapStart, 
            g_pQForkControl->availableBlocksInHeap * g_pQForkControl->heapBlockSize, 
            PAGE_WRITECOPY, 
            &oldProtect) == FALSE ) {
            throw std::system_error(
                GetLastError(),
                system_category(),
                "BeginForkOperation: VirtualProtect failed");
        }

777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
        // ensure events are in the correst state
        if (ResetEvent(g_pQForkControl->operationComplete) == FALSE ) {
            throw std::system_error(
                GetLastError(),
                system_category(), 
                "BeginForkOperation: ResetEvent() failed.");
        }
        if (ResetEvent(g_pQForkControl->operationFailed) == FALSE ) {
            throw std::system_error(
                GetLastError(),
                system_category(), 
                "BeginForkOperation: ResetEvent() failed.");
        }
        if (ResetEvent(g_pQForkControl->startOperation) == FALSE ) {
            throw std::system_error(
                GetLastError(),
                system_category(),
                "BeginForkOperation: ResetEvent() failed.");
        }
        if (ResetEvent(g_pQForkControl->forkedProcessReady) == FALSE) {
            throw std::system_error(
                GetLastError(),
                system_category(),
                "BeginForkOperation: ResetEvent() failed.");
        }
        if (ResetEvent(g_pQForkControl->terminateForkedProcess) == FALSE) {
            throw std::system_error(
                GetLastError(), 
                system_category(),
                "BeginForkOperation: ResetEvent() failed.");
        }

809
        // Launch the "forked" process
810 811
        char fileName[MAX_PATH];
        if (0 == GetModuleFileNameA(NULL, fileName, MAX_PATH)) {
812 813 814 815 816 817
            throw system_error(
                GetLastError(),
                system_category(),
                "Failed to get module name.");
        }

818 819 820 821
        STARTUPINFOA si;
        memset(&si,0, sizeof(STARTUPINFOA));
        si.cb = sizeof(STARTUPINFOA);
        char arguments[_MAX_PATH];
822 823
        memset(arguments,0,_MAX_PATH);
        PROCESS_INFORMATION pi;
824
        sprintf_s(arguments, _MAX_PATH, "%s %llu %lu", qforkFlag, (uint64_t)g_hQForkControlFileMap, GetCurrentProcessId());
825
        if (FALSE == CreateProcessA(fileName, arguments, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
826 827 828 829 830 831
            throw system_error( 
                GetLastError(),
                system_category(),
                "Problem creating slave process" );
        }
        (*childPID) = pi.dwProcessId;
832 833
		g_hForkedProcess = pi.hProcess;
		CloseHandle(pi.hThread);
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848

        // wait for "forked" process to map memory
        if(WaitForSingleObject(g_pQForkControl->forkedProcessReady,10000) != WAIT_OBJECT_0) {
            throw system_error(
                GetLastError(),
                system_category(),
                "Forked Process did not respond in a timely manner.");
        }

        // signal the 2nd process that we want to do some work
        SetEvent(g_pQForkControl->startOperation);

        return TRUE;
    }
    catch(std::system_error syserr) {
849
        printf("BeginForkOperation: system error caught. error code=0x%08x, message=%s\n", syserr.code().value(), syserr.what());
850
    }
851 852
    catch(std::runtime_error runerr) {
        printf("BeginForkOperation: runtime error caught. message=%s\n", runerr.what());
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
    }
    catch(...) {
        printf("BeginForkOperation: other exception caught.\n");
    }
    return FALSE;
}

OperationStatus GetForkOperationStatus() {
    if (WaitForSingleObject(g_pQForkControl->operationComplete, 0) == WAIT_OBJECT_0) {
        return OperationStatus::osCOMPLETE;
    }

    if (WaitForSingleObject(g_pQForkControl->operationFailed, 0) == WAIT_OBJECT_0) {
        return OperationStatus::osFAILED;
    }

    if (WaitForSingleObject(g_pQForkControl->forkedProcessReady, 0) == WAIT_OBJECT_0) {
        return OperationStatus::osINPROGRESS;
    }
    
    return OperationStatus::osUNSTARTED;
}

BOOL AbortForkOperation()
{
    try {
        if( g_hForkedProcess != 0 )
        {
            if (TerminateProcess(g_hForkedProcess, 1) == FALSE) {
                throw std::system_error(
                    GetLastError(),
                    system_category(),
                    "EndForkOperation: Killing forked process failed.");
            }
            g_hForkedProcess = 0;
888
			CloseHandle(g_hForkedProcess);
889 890
        }

891
        return EndForkOperation(NULL);
892 893
    }
    catch(std::system_error syserr) {
894
        printf("0x%08x - %s\n", syserr.code().value(), syserr.what());
895 896 897 898 899 900 901 902 903 904 905 906

        // If we can not properly restore fork state, then another fork operation is not possible. 
        exit(1);
    }
    catch( ... ) {
        printf("Some other exception caught in EndForkOperation().\n");
        exit(1);
    }
    return FALSE;
}


907
BOOL EndForkOperation(int * pExitCode) {
908 909 910 911 912 913 914 915 916 917 918 919
    try {
        SetEvent(g_pQForkControl->terminateForkedProcess);
        if( g_hForkedProcess != 0 )
        {
            if (WaitForSingleObject(g_hForkedProcess, cDeadForkWait) == WAIT_TIMEOUT) {
                if (TerminateProcess(g_hForkedProcess, 1) == FALSE) {
                    throw std::system_error(
                        GetLastError(),
                        system_category(),
                        "EndForkOperation: Killing forked process failed.");
                }
            }
920 921 922 923 924
			
			if (pExitCode != NULL) {
				GetExitCodeProcess(g_hForkedProcess, (DWORD*)pExitCode);
			}

925
			CloseHandle(g_hForkedProcess);
926
			g_hForkedProcess = 0;
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
        }

        if (ResetEvent(g_pQForkControl->operationComplete) == FALSE ) {
            throw std::system_error(
                GetLastError(),
                system_category(), 
                "EndForkOperation: ResetEvent() failed.");
        }
        if (ResetEvent(g_pQForkControl->operationFailed) == FALSE ) {
            throw std::system_error(
                GetLastError(),
                system_category(), 
                "EndForkOperation: ResetEvent() failed.");
        }
        if (ResetEvent(g_pQForkControl->startOperation) == FALSE ) {
            throw std::system_error(
                GetLastError(),
                system_category(),
                "EndForkOperation: ResetEvent() failed.");
        }
        if (ResetEvent(g_pQForkControl->forkedProcessReady) == FALSE) {
            throw std::system_error(
                GetLastError(),
                system_category(),
                "EndForkOperation: ResetEvent() failed.");
        }
        if (ResetEvent(g_pQForkControl->terminateForkedProcess) == FALSE) {
            throw std::system_error(
                GetLastError(), 
                system_category(),
                "EndForkOperation: ResetEvent() failed.");
        }

        // restore protection constants on shared memory blocks 
        DWORD oldProtect = 0;
        if (VirtualProtect(g_pQForkControl, sizeof(QForkControl), PAGE_READWRITE, &oldProtect) == FALSE) {
            throw std::system_error(
                GetLastError(), 
                system_category(),
                "EndForkOperation: VirtualProtect failed.");
        }
        if (VirtualProtect( 
            g_pQForkControl->heapStart, 
            g_pQForkControl->availableBlocksInHeap * g_pQForkControl->heapBlockSize, 
            PAGE_READWRITE, 
            &oldProtect) == FALSE ) {
            throw std::system_error(
                GetLastError(),
                system_category(),
                "EndForkOperation: VirtualProtect failed.");
        }

        //
        // What can be done to unify COW pages back into the section?
        //
        // 1. find the modified pages
        // 2. copy the modified pages into a buffer
        // 3. close the section map (discarding local changes)
        // 4. reopen the section map
        // 5. copy modified pages over reopened section map
        //
        // This assumes that the forked process is reasonably quick, such that this copy is not a huge burden.
        //
        typedef vector<INT_PTR> COWList;
        typedef COWList::iterator COWListIterator;
        COWList cowList;
        HANDLE hProcess = GetCurrentProcess();
        size_t mmSize = g_pQForkControl->availableBlocksInHeap * g_pQForkControl->heapBlockSize;
        int pages = (int)(mmSize / pageSize);
        PSAPI_WORKING_SET_EX_INFORMATION* pwsi =
            new PSAPI_WORKING_SET_EX_INFORMATION[pages];
        if (pwsi == NULL) {
            throw new system_error(
                GetLastError(),
                system_category(),
                "pwsi == NULL");
        }
        memset(pwsi, 0, sizeof(PSAPI_WORKING_SET_EX_INFORMATION) * pages);
        int virtualLockFailures = 0;
        for (int page = 0; page < pages; page++) {
            pwsi[page].VirtualAddress = (BYTE*)g_pQForkControl->heapStart + page * pageSize;
        }
                
        if (QueryWorkingSetEx( 
                hProcess, 
                pwsi, 
                sizeof(PSAPI_WORKING_SET_EX_INFORMATION) * pages) == FALSE) {
            throw system_error( 
                GetLastError(),
                system_category(),
                "QueryWorkingSet failure");
        }

        for (int page = 0; page < pages; page++) {
            if (pwsi[page].VirtualAttributes.Valid == 1) {
                // A 0 share count indicates a COW page
                if (pwsi[page].VirtualAttributes.ShareCount == 0) {
                    cowList.push_back(page);
                }
            }
        }

        if (cowList.size() > 0) {
            LPBYTE cowBuffer = (LPBYTE)malloc(cowList.size() * pageSize);
            int bufPageIndex = 0;
            for (COWListIterator cli = cowList.begin(); cli != cowList.end(); cli++) {
                memcpy(
                    cowBuffer + (bufPageIndex * pageSize),
                    (BYTE*)g_pQForkControl->heapStart + ((*cli) * pageSize),
                    pageSize);
                bufPageIndex++;
            }

            delete [] pwsi;
            pwsi = NULL;

            // discard local changes
            if (UnmapViewOfFile(g_pQForkControl->heapStart) == FALSE) {
                throw std::system_error(
                    GetLastError(),
                    system_category(),
                    "EndForkOperation: UnmapViewOfFile failed.");
            }
            g_pQForkControl->heapStart = 
                MapViewOfFileEx(
                    g_pQForkControl->heapMemoryMap,
                    FILE_MAP_ALL_ACCESS,
                    0,0,                            
                    0,  
                    g_pQForkControl->heapStart);
            if (g_pQForkControl->heapStart == NULL) {
                throw std::system_error(
                    GetLastError(),
                    system_category(),
                    "EndForkOperation: Remapping ForkControl block failed.");
            }

            // copied back local changes to remapped view
            bufPageIndex = 0;
            for (COWListIterator cli = cowList.begin(); cli != cowList.end(); cli++) {
                memcpy(
                    (BYTE*)g_pQForkControl->heapStart + ((*cli) * pageSize),
                    cowBuffer + (bufPageIndex * pageSize),
                    pageSize);
                bufPageIndex++;
            }
            delete cowBuffer;
            cowBuffer = NULL;
        }

        // now do the same with qfork control
        LPVOID controlCopy = malloc(sizeof(QForkControl));
        if(controlCopy == NULL) {
            throw std::system_error(
                GetLastError(),
                system_category(),
                "EndForkOperation: allocation failed.");
        }
        memcpy(controlCopy, g_pQForkControl, sizeof(QForkControl));
        if (UnmapViewOfFile(g_pQForkControl) == FALSE) {
            throw std::system_error(
                GetLastError(),
                system_category(),
                "EndForkOperation: UnmapViewOfFile failed.");
        }
        g_pQForkControl = (QForkControl*)
            MapViewOfFileEx(
                g_hQForkControlFileMap,
                FILE_MAP_ALL_ACCESS,
                0,0,                            
                0,  
                g_pQForkControl);
        if (g_pQForkControl == NULL) {
            throw std::system_error(
                GetLastError(), 
                system_category(), 
                "EndForkOperation: Remapping ForkControl failed.");
        }
        memcpy(g_pQForkControl, controlCopy,sizeof(QForkControl));
        delete controlCopy;
        controlCopy = NULL;

        return TRUE;
    }
    catch(std::system_error syserr) {
1112
        printf("0x%08x - %s\n", syserr.code().value(), syserr.what());
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129

        // If we can not properly restore fork state, then another fork operation is not possible. 
        exit(1);
    }
    catch( ... ) {
        printf("Some other exception caught in EndForkOperation().\n");
        exit(1);
    }
    return FALSE;
}

int blocksMapped = 0;
int totalAllocCalls = 0;
int totalFreeCalls = 0;

LPVOID AllocHeapBlock(size_t size, BOOL allocateHigh) {
    totalAllocCalls++;
1130
    LPVOID retPtr = (LPVOID)NULL;
1131 1132 1133 1134 1135 1136 1137 1138
    if (size % g_pQForkControl->heapBlockSize != 0 ) {
        errno = EINVAL;
        return retPtr;
    }
    int contiguousBlocksToAllocate = (int)(size / g_pQForkControl->heapBlockSize);

    size_t mapped = 0;
    int startIndex = allocateHigh ? g_pQForkControl->availableBlocksInHeap - 1 : contiguousBlocksToAllocate - 1;
1139
    int endIndex = allocateHigh ? -1 : g_pQForkControl->availableBlocksInHeap - contiguousBlocksToAllocate + 1;
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
    int direction = allocateHigh ? -1 : 1;
    int blockIndex = 0;
    int contiguousBlocksFound = 0;
    for(blockIndex = startIndex; 
        blockIndex != endIndex; 
        blockIndex += direction) {
        for (int n = 0; n < contiguousBlocksToAllocate; n++) {
            if (g_pQForkControl->heapBlockMap[blockIndex + n * direction] == BlockState::bsUNMAPPED) {
                contiguousBlocksFound++;
            }
            else {
                contiguousBlocksFound = 0;
                break;
            }
        }
        if (contiguousBlocksFound == contiguousBlocksToAllocate) {
            break;
        }
    }

    if (contiguousBlocksFound == contiguousBlocksToAllocate) {
        int allocationStart = blockIndex + (allocateHigh ? 1 - contiguousBlocksToAllocate : 0);
        LPVOID blockStart = 
            reinterpret_cast<byte*>(g_pQForkControl->heapStart) + 
            (g_pQForkControl->heapBlockSize * allocationStart);
        for(int n = 0; n < contiguousBlocksToAllocate; n++ ) {
            g_pQForkControl->heapBlockMap[allocationStart+n] = BlockState::bsMAPPED;
            blocksMapped++;
            mapped += g_pQForkControl->heapBlockSize; 
        }
        retPtr = blockStart;
    }
    else {
        errno = ENOMEM;
    }

    return retPtr;
}

BOOL FreeHeapBlock(LPVOID block, size_t size)
{
    totalFreeCalls++;
    if (size == 0) {
        return FALSE;
    }
J
Jonathan Pickett 已提交
1185

1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210
    INT_PTR ptrDiff = reinterpret_cast<byte*>(block) - reinterpret_cast<byte*>(g_pQForkControl->heapStart);
    if (ptrDiff < 0 || (ptrDiff % g_pQForkControl->heapBlockSize) != 0) {
        return FALSE;
    }

    int blockIndex = (int)(ptrDiff / g_pQForkControl->heapBlockSize);
    if (blockIndex >= g_pQForkControl->availableBlocksInHeap) {
        return FALSE;
    }

    int contiguousBlocksToFree = (int)(size / g_pQForkControl->heapBlockSize);

    if (VirtualUnlock(block, size) == FALSE) {
        DWORD err = GetLastError();
        if (err != ERROR_NOT_LOCKED) {
            return FALSE;
        }
    };
    for (int n = 0; n < contiguousBlocksToFree; n++ ) {
        blocksMapped--;
        g_pQForkControl->heapBlockMap[blockIndex + n] = BlockState::bsUNMAPPED;
    }
    return TRUE;
}

1211

1212 1213 1214 1215 1216 1217 1218
extern "C"
{
    // The external main() is redefined as redis_main() by Win32_QFork.h.
    // The CRT will call this replacement main() before the previous main()
    // is invoked so that the QFork allocator can be setup prior to anything 
    // Redis will allocate.
    int main(int argc, char* argv[]) {
1219
		try {
1220
#ifdef DEBUG_WITH_PROCMON
1221 1222 1223 1224 1225 1226 1227 1228 1229
			hProcMonDevice = 
				CreateFile( 
				L"\\\\.\\Global\\ProcmonDebugLogger", 
				GENERIC_READ|GENERIC_WRITE, 
				FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, 
				NULL, 
				OPEN_EXISTING, 
				FILE_ATTRIBUTE_NORMAL, 
				NULL );
1230 1231
#endif

1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243
			// service commands do not launch an instance of redis directly
			if (HandleServiceCommands(argc, argv) == TRUE)
				return 0;

			StartupStatus status = QForkStartup(argc, argv);
			if (status == ssCONTINUE_AS_MASTER) {
				int retval = redis_main(argc, argv);
				QForkShutdown();
				return retval;
			} else if (status == ssSLAVE_EXIT) {
				// slave is done - clean up and exit
				QForkShutdown();
1244
				return g_SlaveExitCode;
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258
			} else if (status == ssFAILED) {
				// master or slave failed initialization
				return 1;
			} else {
				// unexpected status return
				return 2;
			}
		} catch (std::system_error syserr) {
			printf("main: system error caught. error code=0x%08x, message=%s\n", syserr.code().value(), syserr.what());
		} catch (std::runtime_error runerr) {
			printf("main: runtime error caught. message=%s\n", runerr.what());
		} catch (...) {
			printf("main: other exception caught.\n");
		}
1259 1260
    }
}