hotspot/src/os/linux/vm/os_linux.cpp
changeset 31963 641ed52732ec
parent 31610 b05ea6f92971
child 32070 8f5a14b884e2
equal deleted inserted replaced
31856:614d6786ba55 31963:641ed52732ec
  2041 // distribution information and the system-release file seems to be an old
  2041 // distribution information and the system-release file seems to be an old
  2042 // standard that has been replaced by the lsb-release and os-release files.
  2042 // standard that has been replaced by the lsb-release and os-release files.
  2043 // Searching for the debian_version file is the last resort.  It contains
  2043 // Searching for the debian_version file is the last resort.  It contains
  2044 // an informative string like "6.0.6" or "wheezy/sid". Because of this
  2044 // an informative string like "6.0.6" or "wheezy/sid". Because of this
  2045 // "Debian " is printed before the contents of the debian_version file.
  2045 // "Debian " is printed before the contents of the debian_version file.
       
  2046 
       
  2047 const char* distro_files[] = {
       
  2048   "/etc/oracle-release",
       
  2049   "/etc/mandriva-release",
       
  2050   "/etc/mandrake-release",
       
  2051   "/etc/sun-release",
       
  2052   "/etc/redhat-release",
       
  2053   "/etc/lsb-release",
       
  2054   "/etc/SuSE-release",
       
  2055   "/etc/turbolinux-release",
       
  2056   "/etc/gentoo-release",
       
  2057   "/etc/ltib-release",
       
  2058   "/etc/angstrom-version",
       
  2059   "/etc/system-release",
       
  2060   "/etc/os-release",
       
  2061   NULL };
       
  2062 
  2046 void os::Linux::print_distro_info(outputStream* st) {
  2063 void os::Linux::print_distro_info(outputStream* st) {
  2047   if (!_print_ascii_file("/etc/oracle-release", st) &&
  2064   for (int i = 0;; i++) {
  2048       !_print_ascii_file("/etc/mandriva-release", st) &&
  2065     const char* file = distro_files[i];
  2049       !_print_ascii_file("/etc/mandrake-release", st) &&
  2066     if (file == NULL) {
  2050       !_print_ascii_file("/etc/sun-release", st) &&
  2067       break;  // done
  2051       !_print_ascii_file("/etc/redhat-release", st) &&
  2068     }
  2052       !_print_ascii_file("/etc/lsb-release", st) &&
  2069     // If file prints, we found it.
  2053       !_print_ascii_file("/etc/SuSE-release", st) &&
  2070     if (_print_ascii_file(file, st)) {
  2054       !_print_ascii_file("/etc/turbolinux-release", st) &&
  2071       return;
  2055       !_print_ascii_file("/etc/gentoo-release", st) &&
  2072     }
  2056       !_print_ascii_file("/etc/ltib-release", st) &&
  2073   }
  2057       !_print_ascii_file("/etc/angstrom-version", st) &&
  2074 
  2058       !_print_ascii_file("/etc/system-release", st) &&
  2075   if (file_exists("/etc/debian_version")) {
  2059       !_print_ascii_file("/etc/os-release", st)) {
  2076     st->print("Debian ");
  2060 
  2077     _print_ascii_file("/etc/debian_version", st);
  2061     if (file_exists("/etc/debian_version")) {
  2078   } else {
  2062       st->print("Debian ");
  2079     st->print("Linux");
  2063       _print_ascii_file("/etc/debian_version", st);
  2080   }
       
  2081   st->cr();
       
  2082 }
       
  2083 
       
  2084 static void parse_os_info(char* distro, size_t length, const char* file) {
       
  2085   FILE* fp = fopen(file, "r");
       
  2086   if (fp != NULL) {
       
  2087     char buf[256];
       
  2088     // get last line of the file.
       
  2089     while (fgets(buf, sizeof(buf), fp)) { }
       
  2090     // Edit out extra stuff in expected ubuntu format
       
  2091     if (strstr(buf, "DISTRIB_DESCRIPTION=") != NULL) {
       
  2092       char* ptr = strstr(buf, "\"");  // the name is in quotes
       
  2093       if (ptr != NULL) {
       
  2094         ptr++; // go beyond first quote
       
  2095         char* nl = strchr(ptr, '\"');
       
  2096         if (nl != NULL) *nl = '\0';
       
  2097         strncpy(distro, ptr, length);
       
  2098       } else {
       
  2099         ptr = strstr(buf, "=");
       
  2100         ptr++; // go beyond equals then
       
  2101         char* nl = strchr(ptr, '\n');
       
  2102         if (nl != NULL) *nl = '\0';
       
  2103         strncpy(distro, ptr, length);
       
  2104       }
  2064     } else {
  2105     } else {
  2065       st->print("Linux");
  2106       // if not in expected Ubuntu format, print out whole line minus \n
  2066     }
  2107       char* nl = strchr(buf, '\n');
  2067   }
  2108       if (nl != NULL) *nl = '\0';
  2068   st->cr();
  2109       strncpy(distro, buf, length);
       
  2110     }
       
  2111     // close distro file
       
  2112     fclose(fp);
       
  2113   }
       
  2114 }
       
  2115 
       
  2116 void os::get_summary_os_info(char* buf, size_t buflen) {
       
  2117   for (int i = 0;; i++) {
       
  2118     const char* file = distro_files[i];
       
  2119     if (file == NULL) {
       
  2120       break; // ran out of distro_files
       
  2121     }
       
  2122     if (file_exists(file)) {
       
  2123       parse_os_info(buf, buflen, file);
       
  2124       return;
       
  2125     }
       
  2126   }
       
  2127   // special case for debian
       
  2128   if (file_exists("/etc/debian_version")) {
       
  2129     strncpy(buf, "Debian ", buflen);
       
  2130     parse_os_info(&buf[7], buflen-7, "/etc/debian_version");
       
  2131   } else {
       
  2132     strncpy(buf, "Linux", buflen);
       
  2133   }
  2069 }
  2134 }
  2070 
  2135 
  2071 void os::Linux::print_libversion_info(outputStream* st) {
  2136 void os::Linux::print_libversion_info(outputStream* st) {
  2072   // libc, pthread
  2137   // libc, pthread
  2073   st->print("libc:");
  2138   st->print("libc:");
  2146     st->print("\n/proc/cpuinfo:\n");
  2211     st->print("\n/proc/cpuinfo:\n");
  2147     if (!_print_ascii_file("/proc/cpuinfo", st)) {
  2212     if (!_print_ascii_file("/proc/cpuinfo", st)) {
  2148       st->print_cr("  <Not Available>");
  2213       st->print_cr("  <Not Available>");
  2149     }
  2214     }
  2150   }
  2215   }
       
  2216 }
       
  2217 
       
  2218 const char* search_string = IA32_ONLY("model name") AMD64_ONLY("model name")
       
  2219                             IA64_ONLY("") SPARC_ONLY("cpu")
       
  2220                             ARM32_ONLY("Processor") PPC_ONLY("Processor") AARCH64_ONLY("Processor");
       
  2221 
       
  2222 // Parses the cpuinfo file for string representing the model name.
       
  2223 void os::get_summary_cpu_info(char* cpuinfo, size_t length) {
       
  2224   FILE* fp = fopen("/proc/cpuinfo", "r");
       
  2225   if (fp != NULL) {
       
  2226     while (!feof(fp)) {
       
  2227       char buf[256];
       
  2228       if (fgets(buf, sizeof(buf), fp)) {
       
  2229         char* start = strstr(buf, search_string);
       
  2230         if (start != NULL) {
       
  2231           char *ptr = start + strlen(search_string);
       
  2232           char *end = buf + strlen(buf);
       
  2233           while (ptr != end) {
       
  2234              // skip whitespace and colon for the rest of the name.
       
  2235              if (*ptr != ' ' && *ptr != '\t' && *ptr != ':') {
       
  2236                break;
       
  2237              }
       
  2238              ptr++;
       
  2239           }
       
  2240           if (ptr != end) {
       
  2241             // reasonable string, get rid of newline and keep the rest
       
  2242             char* nl = strchr(buf, '\n');
       
  2243             if (nl != NULL) *nl = '\0';
       
  2244             strncpy(cpuinfo, ptr, length);
       
  2245             fclose(fp);
       
  2246             return;
       
  2247           }
       
  2248         }
       
  2249       }
       
  2250     }
       
  2251     fclose(fp);
       
  2252   }
       
  2253   // cpuinfo not found or parsing failed, just print generic string.  The entire
       
  2254   // /proc/cpuinfo file will be printed later in the file (or enough of it for x86)
       
  2255   strncpy(cpuinfo, IA32_ONLY("x86_32") AMD64_ONLY("x86_32")
       
  2256                    IA64_ONLY("IA64") SPARC_ONLY("sparcv9")
       
  2257                    ARM32_ONLY("ARM") PPC_ONLY("PPC64") AARCH64_ONLY("AArch64"), length);
  2151 }
  2258 }
  2152 
  2259 
  2153 void os::print_siginfo(outputStream* st, void* siginfo) {
  2260 void os::print_siginfo(outputStream* st, void* siginfo) {
  2154   const siginfo_t* si = (const siginfo_t*)siginfo;
  2261   const siginfo_t* si = (const siginfo_t*)siginfo;
  2155 
  2262