12
12
*/
13
13
#include "postgres.h"
14
14
15
- #include "lib/stringinfo.h"
16
15
#include "miscadmin.h"
17
16
#include "storage/dsm.h"
18
17
#include "storage/ipc.h"
@@ -25,65 +24,6 @@ static Size UsedShmemSegSize = 0;
25
24
static bool EnableLockPagesPrivilege (int elevel );
26
25
static void pgwin32_SharedMemoryDelete (int status , Datum shmId );
27
26
28
- static const char *
29
- mi_type (DWORD code )
30
- {
31
- switch (code )
32
- {
33
- case MEM_IMAGE :
34
- return "img" ;
35
- case MEM_MAPPED :
36
- return "map" ;
37
- case MEM_PRIVATE :
38
- return "prv" ;
39
- }
40
- return "???" ;
41
- }
42
-
43
- static const char *
44
- mi_state (DWORD code )
45
- {
46
- switch (code )
47
- {
48
- case MEM_COMMIT :
49
- return "commit" ;
50
- case MEM_FREE :
51
- return "free " ;
52
- case MEM_RESERVE :
53
- return "reserv" ;
54
- }
55
- return "???" ;
56
- }
57
-
58
- /*
59
- * Append memory dump to buf. To avoid affecting the memory map mid-run,
60
- * buf should be preallocated to be bigger than needed.
61
- */
62
- static void
63
- dumpmem (StringInfo buf , const char * reason )
64
- {
65
- char * addr = 0 ;
66
- MEMORY_BASIC_INFORMATION mi ;
67
-
68
- appendStringInfo (buf , "%s memory map:\n" , reason );
69
- do
70
- {
71
- memset (& mi , 0 , sizeof (mi ));
72
- if (!VirtualQuery (addr , & mi , sizeof (mi )))
73
- {
74
- if (GetLastError () == ERROR_INVALID_PARAMETER )
75
- break ;
76
- appendStringInfo (buf , "VirtualQuery failed: %lu\n" , GetLastError ());
77
- break ;
78
- }
79
- appendStringInfo (buf , "0x%p+0x%p %s (alloc 0x%p) %s\n" ,
80
- mi .BaseAddress , (void * ) mi .RegionSize ,
81
- mi_type (mi .Type ), mi .AllocationBase ,
82
- mi_state (mi .State ));
83
- addr += mi .RegionSize ;
84
- } while (addr > 0 );
85
- }
86
-
87
27
/*
88
28
* Generate shared memory segment name. Expand the data directory, to generate
89
29
* an identifier unique for this data directory. Then replace all backslashes
@@ -251,7 +191,6 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port,
251
191
SIZE_T largePageSize = 0 ;
252
192
Size orig_size = size ;
253
193
DWORD flProtect = PAGE_READWRITE ;
254
- MEMORY_BASIC_INFORMATION info ;
255
194
256
195
/* Room for a header? */
257
196
Assert (size > MAXALIGN (sizeof (PGShmemHeader )));
@@ -420,14 +359,6 @@ PGSharedMemoryCreate(Size size, bool makePrivate, int port,
420
359
/* Register on-exit routine to delete the new segment */
421
360
on_shmem_exit (pgwin32_SharedMemoryDelete , PointerGetDatum (hmap2 ));
422
361
423
- /* Log information about the segment's virtual memory use */
424
- if (VirtualQuery (memAddress , & info , sizeof (info )) != 0 )
425
- elog (LOG , "mapped shared memory segment at %p, requested size 0x%zx, mapped size 0x%zx" ,
426
- memAddress , size , info .RegionSize );
427
- else
428
- elog (LOG , "VirtualQuery(%p) failed: error code %lu" ,
429
- memAddress , GetLastError ());
430
-
431
362
* shim = hdr ;
432
363
return hdr ;
433
364
}
@@ -448,55 +379,28 @@ PGSharedMemoryReAttach(void)
448
379
{
449
380
PGShmemHeader * hdr ;
450
381
void * origUsedShmemSegAddr = UsedShmemSegAddr ;
451
- StringInfoData buf ;
452
382
453
383
Assert (UsedShmemSegAddr != NULL );
454
384
Assert (IsUnderPostmaster );
455
385
456
- /* Ensure buf is big enough that it won't grow mid-operation */
457
- initStringInfo (& buf );
458
- enlargeStringInfo (& buf , 128 * 1024 );
459
- /* ... and let's just be sure all that space is committed */
460
- memset (buf .data , 0 , buf .maxlen );
461
-
462
- /* Test: see if this lets the process address space quiesce */
463
- pg_usleep (1000000L );
464
-
465
- dumpmem (& buf , "before VirtualFree" );
466
-
467
386
/*
468
387
* Release memory region reservation that was made by the postmaster
469
388
*/
470
389
if (VirtualFree (UsedShmemSegAddr , 0 , MEM_RELEASE ) == 0 )
471
390
elog (FATAL , "failed to release reserved memory region (addr=%p): error code %lu" ,
472
391
UsedShmemSegAddr , GetLastError ());
473
392
474
- dumpmem (& buf , "after VirtualFree" );
475
-
476
393
hdr = (PGShmemHeader * ) MapViewOfFileEx (UsedShmemSegID , FILE_MAP_READ | FILE_MAP_WRITE , 0 , 0 , 0 , UsedShmemSegAddr );
477
394
if (!hdr )
478
- {
479
- DWORD maperr = GetLastError ();
480
-
481
- dumpmem (& buf , "after failed MapViewOfFileEx" );
482
- elog (LOG , "%s" , buf .data );
483
-
484
395
elog (FATAL , "could not reattach to shared memory (key=%p, addr=%p): error code %lu" ,
485
- UsedShmemSegID , UsedShmemSegAddr , maperr );
486
- }
487
-
488
- dumpmem (& buf , "after MapViewOfFileEx" );
489
- elog (LOG , "%s" , buf .data );
490
-
396
+ UsedShmemSegID , UsedShmemSegAddr , GetLastError ());
491
397
if (hdr != origUsedShmemSegAddr )
492
398
elog (FATAL , "reattaching to shared memory returned unexpected address (got %p, expected %p)" ,
493
399
hdr , origUsedShmemSegAddr );
494
400
if (hdr -> magic != PGShmemMagic )
495
401
elog (FATAL , "reattaching to shared memory returned non-PostgreSQL memory" );
496
402
dsm_set_control_handle (hdr -> dsm_control );
497
403
498
- pfree (buf .data );
499
-
500
404
UsedShmemSegAddr = hdr ; /* probably redundant */
501
405
}
502
406
0 commit comments