Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8321971

Improve the user name detection logic in perfMemory get_user_name_slow

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Unresolved
    • Icon: P4 P4
    • None
    • None
    • hotspot

      The hotspot implementation of the "attach" mechanism, before doing the actual attaching, checks if the target JVM is attachable. To do that, there's a piece of code which tries to memory map a file that corresponds to the target JVM process. The name of the file it memory maps, is the pid of the target JVM. The location of this file is "<tmpdir>/hsperfdata_<user>/<pid>". <tmpdir> is OS specific and differs on each OS. The "user" part in the "hsperfdata_<user>" directory name is unknown to the client (code) which is trying to attach to the target JVM. So in order to identify the target JVM's user name, the native code in src/hotspot/os/posix/perfMemory_posix.cpp (and its corresponding Windows version) has a method called get_user_name_slow which at a high level does this:


      long pid = target JVM's pid
      tmpdir = getTemporaryDirectoryRoot();
      tmpdirpointer = open(tmpdir) <-- sys call
      for (each entry in tmpdir) { <-- sys call(s)
          if (!entry.name.startsWith("hsperfdata_")) {
              // skip and check next
              continue;
          }
          subdir = entry.name;
          subdirpointer = open(subdir); <-- sys call
          ... // do some checks on subdir
          for (each entry in subdir) { <-- sys call(s)
              if (!subdirentry.name.equals(String.valueOf(pid))) {
                  // not the file of interest, skip and check next
                  continue;
              }
              // found the relevant file corresponding to the target JVM.
              // do some additional checks and hold on to this matching
              // file

          }
      }


      The outer loop is understandable because you don't know what directory name you are looking for since the <user> part is unknown. However, the nested one loop, which reads the entries of the subdirectory under hsperfdata_xxx to try and locate a file by the name <pid> can be avoided, since we know the file name that we are looking for in this sub directory. i.e. once you have found a hsperfdata_xxx directory, you can just look for a file named <pid> within that directory instead of iterating each entry in that subdir and trying to then compare the name of the returned entry with the pid.

            jpai Jaikiran Pai
            jpai Jaikiran Pai
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: