@@ -73,18 +73,6 @@ static char* create_standard_memory(size_t size) {
73
73
return mapAddress;
74
74
}
75
75
76
- // delete the PerfData memory region
77
- //
78
- static void delete_standard_memory (char * addr, size_t size) {
79
-
80
- // there are no persistent external resources to cleanup for standard
81
- // memory. since DestroyJavaVM does not support unloading of the JVM,
82
- // cleanup of the memory resource is not performed. The memory will be
83
- // reclaimed by the OS upon termination of the process.
84
- //
85
- return ;
86
- }
87
-
88
76
// save the specified memory region to the given file
89
77
//
90
78
// Note: this function might be called from signal handler (by os::abort()),
@@ -656,16 +644,17 @@ static void remove_file(const char* path) {
656
644
}
657
645
658
646
659
- // cleanup stale shared memory resources
647
+ // cleanup stale shared memory files
660
648
//
661
649
// This method attempts to remove all stale shared memory files in
662
650
// the named user temporary directory. It scans the named directory
663
- // for files matching the pattern ^$[0-9]*$. For each file found, the
664
- // process id is extracted from the file name and a test is run to
665
- // determine if the process is alive. If the process is not alive,
666
- // any stale file resources are removed.
651
+ // for files matching the pattern ^$[0-9]*$.
667
652
//
668
- static void cleanup_sharedmem_resources (const char * dirname) {
653
+ // This directory should be used only by JVM processes owned by the
654
+ // current user to store PerfMemory files. Any other files found
655
+ // in this directory may be removed.
656
+ //
657
+ static void cleanup_sharedmem_files (const char * dirname) {
669
658
670
659
int saved_cwd_fd;
671
660
// open the directory and set the current working directory to it
@@ -675,50 +664,56 @@ static void cleanup_sharedmem_resources(const char* dirname) {
675
664
return ;
676
665
}
677
666
678
- // for each entry in the directory that matches the expected file
679
- // name pattern, determine if the file resources are stale and if
680
- // so, remove the file resources. Note, instrumented HotSpot processes
681
- // for this user may start and/or terminate during this search and
682
- // remove or create new files in this directory. The behavior of this
683
- // loop under these conditions is dependent upon the implementation of
684
- // opendir/readdir.
667
+ // For each entry in the directory that matches the expected file
668
+ // name pattern, remove the file if it's determine to be stale
669
+ // Note, instrumented HotSpot processes for this user may start and/or
670
+ // terminate during this search and remove or create new files in this
671
+ // directory. The behavior of this loop under these conditions is dependent
672
+ // upon the implementation of opendir/readdir.
685
673
//
686
674
struct dirent * entry;
687
675
errno = 0 ;
688
676
while ((entry = os::readdir (dirp)) != NULL ) {
689
677
690
- pid_t pid = filename_to_pid (entry->d_name );
678
+ const char * filename = entry->d_name ;
679
+ pid_t pid = filename_to_pid (filename);
691
680
692
681
if (pid == 0 ) {
693
682
694
- if (strcmp (entry-> d_name , " ." ) != 0 && strcmp (entry-> d_name , " .." ) != 0 ) {
683
+ if (strcmp (filename , " ." ) != 0 && strcmp (filename , " .." ) != 0 ) {
695
684
696
685
// attempt to remove all unexpected files, except "." and ".."
697
- unlink (entry-> d_name );
686
+ unlink (filename );
698
687
}
699
688
700
689
errno = 0 ;
701
690
continue ;
702
691
}
703
692
704
- // we now have a file name that converts to a valid integer
705
- // that could represent a process id . if this process id
706
- // matches the current process id or the process is not running,
707
- // then remove the stale file resources.
708
- //
709
- // process liveness is detected by sending signal number 0 to
710
- // the process id (see kill(2)). if kill determines that the
711
- // process does not exist, then the file resources are removed.
712
- // if kill determines that that we don't have permission to
713
- // signal the process, then the file resources are assumed to
714
- // be stale and are removed because the resources for such a
715
- // process should be in a different user specific directory.
716
- //
717
- if ((pid == os::current_process_id ()) ||
718
- (kill (pid, 0 ) == OS_ERR && (errno == ESRCH || errno == EPERM))) {
693
+ // The following code assumes that pid must be in the same
694
+ // namespace as the current process.
695
+ bool stale = false ;
696
+
697
+ if (pid == os::current_process_id ()) {
698
+ // The file was created by a terminated process that happened
699
+ // to have the same pid as the current process.
700
+ stale = true ;
701
+ } else if (kill (pid, 0 ) == OS_ERR) {
702
+ if (errno == ESRCH) {
703
+ // The target process does not exist.
704
+ stale = true ;
705
+ } else if (errno == EPERM) {
706
+ // The file was created by a terminated process that happened
707
+ // to have the same pid as a process not owned by the current user.
708
+ stale = true ;
709
+ }
710
+ }
719
711
720
- unlink (entry->d_name );
712
+ if (stale) {
713
+ log_info (perf, memops)(" Remove stale file %s/%s" , dirname, filename);
714
+ unlink (filename);
721
715
}
716
+
722
717
errno = 0 ;
723
718
}
724
719
@@ -764,13 +759,13 @@ static bool make_user_tmp_dir(const char* dirname) {
764
759
return true ;
765
760
}
766
761
767
- // create the shared memory file resources
762
+ // create the shared memory file
768
763
//
769
764
// This method creates the shared memory file with the given size
770
765
// This method also creates the user specific temporary directory, if
771
766
// it does not yet exist.
772
767
//
773
- static int create_sharedmem_resources (const char * dirname, const char * filename, size_t size) {
768
+ static int create_sharedmem_file (const char * dirname, const char * filename, size_t size) {
774
769
775
770
// make the user temporary directory
776
771
if (!make_user_tmp_dir (dirname)) {
@@ -934,12 +929,13 @@ static char* mmap_create_shared(size_t size) {
934
929
}
935
930
936
931
// cleanup any stale shared memory files
937
- cleanup_sharedmem_resources (dirname);
932
+ cleanup_sharedmem_files (dirname);
938
933
939
934
assert (((size > 0 ) && (size % os::vm_page_size () == 0 )),
940
935
" unexpected PerfMemory region size" );
941
936
942
- fd = create_sharedmem_resources (dirname, short_filename, size);
937
+ log_info (perf, memops)(" Trying to open %s/%s" , dirname, short_filename);
938
+ fd = create_sharedmem_file (dirname, short_filename, size);
943
939
944
940
FREE_C_HEAP_ARRAY (char , user_name);
945
941
FREE_C_HEAP_ARRAY (char , dirname);
@@ -972,6 +968,8 @@ static char* mmap_create_shared(size_t size) {
972
968
// it does not go through os api, the operation has to record from here
973
969
MemTracker::record_virtual_memory_reserve_and_commit ((address)mapAddress, size, CURRENT_PC, mtInternal);
974
970
971
+ log_info (perf, memops)(" Successfully opened" );
972
+
975
973
return mapAddress;
976
974
}
977
975
@@ -993,10 +991,10 @@ static char* create_shared_memory(size_t size) {
993
991
//
994
992
static void delete_shared_memory (char * addr, size_t size) {
995
993
996
- // cleanup the persistent shared memory resources. since DestroyJavaVM does
997
- // not support unloading of the JVM, unmapping of the memory resource is
994
+ // Remove the shared memory file. Since DestroyJavaVM does
995
+ // not support unloading of the JVM, unmapping of the memory region is
998
996
// not performed. The memory will be reclaimed by the OS upon termination of
999
- // the process. The backing store file is deleted from the file system.
997
+ // the process.
1000
998
1001
999
assert (!PerfDisableSharedMem, " shouldn't be here" );
1002
1000
@@ -1196,10 +1194,7 @@ void PerfMemory::delete_memory_region() {
1196
1194
save_memory_to_file (start (), capacity ());
1197
1195
}
1198
1196
1199
- if (PerfDisableSharedMem) {
1200
- delete_standard_memory (start (), capacity ());
1201
- }
1202
- else {
1197
+ if (!PerfDisableSharedMem) {
1203
1198
delete_shared_memory (start (), capacity ());
1204
1199
}
1205
1200
}
0 commit comments