< prev index next >

src/hotspot/share/runtime/os.cpp

Print this page




  72 
  73 OSThread*         os::_starting_thread    = NULL;
  74 address           os::_polling_page       = NULL;
  75 volatile unsigned int os::_rand_seed      = 1;
  76 int               os::_processor_count    = 0;
  77 int               os::_initial_active_processor_count = 0;
  78 size_t            os::_page_sizes[os::page_sizes_max];
  79 
  80 #ifndef PRODUCT
  81 julong os::num_mallocs = 0;         // # of calls to malloc/realloc
  82 julong os::alloc_bytes = 0;         // # of bytes allocated
  83 julong os::num_frees = 0;           // # of calls to free
  84 julong os::free_bytes = 0;          // # of bytes freed
  85 #endif
  86 
  87 static size_t cur_malloc_words = 0;  // current size for MallocMaxTestWords
  88 
  89 DEBUG_ONLY(bool os::_mutex_init_done = false;)
  90 
  91 static time_t get_timezone(const struct tm* time_struct) {



  92 #if defined(_ALLBSD_SOURCE)
  93   return time_struct->tm_gmtoff;
  94 #elif defined(_WINDOWS)
  95   long zone;
  96   _get_timezone(&zone);
  97   return static_cast<time_t>(zone);
  98 #else
  99   return timezone;












 100 #endif


 101 }
 102 
 103 int os::snprintf(char* buf, size_t len, const char* fmt, ...) {
 104   va_list args;
 105   va_start(args, fmt);
 106   int result = os::vsnprintf(buf, len, fmt, args);
 107   va_end(args);
 108   return result;
 109 }
 110 
 111 // Fill in buffer with current local time as an ISO-8601 string.
 112 // E.g., yyyy-mm-ddThh:mm:ss-zzzz.
 113 // Returns buffer, or NULL if it failed.
 114 // This would mostly be a call to
 115 //     strftime(...., "%Y-%m-%d" "T" "%H:%M:%S" "%z", ....)
 116 // except that on Windows the %z behaves badly, so we do it ourselves.
 117 // Also, people wanted milliseconds on there,
 118 // and strftime doesn't do milliseconds.
 119 char* os::iso8601_time(char* buffer, size_t buffer_length, bool utc) {
 120   // Output will be of the form "YYYY-MM-DDThh:mm:ss.mmm+zzzz\0"


 135   // Get the current time
 136   jlong milliseconds_since_19700101 = javaTimeMillis();
 137   const int milliseconds_per_microsecond = 1000;
 138   const time_t seconds_since_19700101 =
 139     milliseconds_since_19700101 / milliseconds_per_microsecond;
 140   const int milliseconds_after_second =
 141     milliseconds_since_19700101 % milliseconds_per_microsecond;
 142   // Convert the time value to a tm and timezone variable
 143   struct tm time_struct;
 144   if (utc) {
 145     if (gmtime_pd(&seconds_since_19700101, &time_struct) == NULL) {
 146       assert(false, "Failed gmtime_pd");
 147       return NULL;
 148     }
 149   } else {
 150     if (localtime_pd(&seconds_since_19700101, &time_struct) == NULL) {
 151       assert(false, "Failed localtime_pd");
 152       return NULL;
 153     }
 154   }
 155   const time_t zone = get_timezone(&time_struct);
 156 
 157   // If daylight savings time is in effect,
 158   // we are 1 hour East of our time zone
 159   const time_t seconds_per_minute = 60;
 160   const time_t minutes_per_hour = 60;
 161   const time_t seconds_per_hour = seconds_per_minute * minutes_per_hour;
 162   time_t UTC_to_local = zone;
 163   if (time_struct.tm_isdst > 0) {
 164     UTC_to_local = UTC_to_local - seconds_per_hour;
 165   }
 166 
 167   // No offset when dealing with UTC
 168   if (utc) {
 169     UTC_to_local = 0;
 170   }
 171 
 172   // Compute the time zone offset.
 173   //    localtime_pd() sets timezone to the difference (in seconds)
 174   //    between UTC and and local time.
 175   //    ISO 8601 says we need the difference between local time and UTC,
 176   //    we change the sign of the localtime_pd() result.
 177   const time_t local_to_UTC = -(UTC_to_local);
 178   // Then we have to figure out if if we are ahead (+) or behind (-) UTC.
 179   char sign_local_to_UTC = '+';
 180   time_t abs_local_to_UTC = local_to_UTC;
 181   if (local_to_UTC < 0) {
 182     sign_local_to_UTC = '-';
 183     abs_local_to_UTC = -(abs_local_to_UTC);
 184   }
 185   // Convert time zone offset seconds to hours and minutes.



 186   const time_t zone_hours = (abs_local_to_UTC / seconds_per_hour);
 187   const time_t zone_min =
 188     ((abs_local_to_UTC % seconds_per_hour) / seconds_per_minute);
 189 
 190   // Print an ISO 8601 date and time stamp into the buffer
 191   const int year = 1900 + time_struct.tm_year;
 192   const int month = 1 + time_struct.tm_mon;
 193   const int printed = jio_snprintf(buffer, buffer_length,
 194                                    "%04d-%02d-%02dT%02d:%02d:%02d.%03d%c%02d%02d",
 195                                    year,
 196                                    month,
 197                                    time_struct.tm_mday,
 198                                    time_struct.tm_hour,
 199                                    time_struct.tm_min,
 200                                    time_struct.tm_sec,
 201                                    milliseconds_after_second,
 202                                    sign_local_to_UTC,
 203                                    zone_hours,
 204                                    zone_min);
 205   if (printed == 0) {




  72 
  73 OSThread*         os::_starting_thread    = NULL;
  74 address           os::_polling_page       = NULL;
  75 volatile unsigned int os::_rand_seed      = 1;
  76 int               os::_processor_count    = 0;
  77 int               os::_initial_active_processor_count = 0;
  78 size_t            os::_page_sizes[os::page_sizes_max];
  79 
  80 #ifndef PRODUCT
  81 julong os::num_mallocs = 0;         // # of calls to malloc/realloc
  82 julong os::alloc_bytes = 0;         // # of bytes allocated
  83 julong os::num_frees = 0;           // # of calls to free
  84 julong os::free_bytes = 0;          // # of bytes freed
  85 #endif
  86 
  87 static size_t cur_malloc_words = 0;  // current size for MallocMaxTestWords
  88 
  89 DEBUG_ONLY(bool os::_mutex_init_done = false;)
  90 
  91 static time_t get_timezone(const struct tm* time_struct) {
  92   // seconds to add to local time to get UTC
  93   time_t offset;
  94 
  95 #if defined(_ALLBSD_SOURCE)
  96   offset = -(time_struct->tm_gmtoff);
  97 #elif defined(_WINDOWS)
  98   long zone;
  99   _get_timezone(&zone);
 100   offset = static_cast<time_t>(zone);
 101 #else
 102   offset = timezone;
 103 #endif
 104 
 105 // tm_gmtoff already includes adjustment for daylight saving
 106 #if !defined(_ALLBSD_SOURCE)
 107   // If daylight savings time is in effect,
 108   // we are 1 hour East of our time zone
 109   const time_t seconds_per_minute = 60;
 110   const time_t minutes_per_hour = 60;
 111   const time_t seconds_per_hour = seconds_per_minute * minutes_per_hour;
 112   if (time_struct->tm_isdst > 0) {
 113     offset = offset - seconds_per_hour;
 114   }
 115 #endif
 116 
 117   return offset;
 118 }
 119 
 120 int os::snprintf(char* buf, size_t len, const char* fmt, ...) {
 121   va_list args;
 122   va_start(args, fmt);
 123   int result = os::vsnprintf(buf, len, fmt, args);
 124   va_end(args);
 125   return result;
 126 }
 127 
 128 // Fill in buffer with current local time as an ISO-8601 string.
 129 // E.g., yyyy-mm-ddThh:mm:ss-zzzz.
 130 // Returns buffer, or NULL if it failed.
 131 // This would mostly be a call to
 132 //     strftime(...., "%Y-%m-%d" "T" "%H:%M:%S" "%z", ....)
 133 // except that on Windows the %z behaves badly, so we do it ourselves.
 134 // Also, people wanted milliseconds on there,
 135 // and strftime doesn't do milliseconds.
 136 char* os::iso8601_time(char* buffer, size_t buffer_length, bool utc) {
 137   // Output will be of the form "YYYY-MM-DDThh:mm:ss.mmm+zzzz\0"


 152   // Get the current time
 153   jlong milliseconds_since_19700101 = javaTimeMillis();
 154   const int milliseconds_per_microsecond = 1000;
 155   const time_t seconds_since_19700101 =
 156     milliseconds_since_19700101 / milliseconds_per_microsecond;
 157   const int milliseconds_after_second =
 158     milliseconds_since_19700101 % milliseconds_per_microsecond;
 159   // Convert the time value to a tm and timezone variable
 160   struct tm time_struct;
 161   if (utc) {
 162     if (gmtime_pd(&seconds_since_19700101, &time_struct) == NULL) {
 163       assert(false, "Failed gmtime_pd");
 164       return NULL;
 165     }
 166   } else {
 167     if (localtime_pd(&seconds_since_19700101, &time_struct) == NULL) {
 168       assert(false, "Failed localtime_pd");
 169       return NULL;
 170     }
 171   }
 172   time_t UTC_to_local = get_timezone(&time_struct);










 173 
 174   // No offset when dealing with UTC
 175   if (utc) {
 176     UTC_to_local = 0;
 177   }
 178 
 179   // Compute the time zone offset.
 180   //    localtime_pd() sets timezone to the difference (in seconds)
 181   //    between UTC and and local time.
 182   //    ISO 8601 says we need the difference between local time and UTC,
 183   //    we change the sign of the localtime_pd() result.
 184   const time_t local_to_UTC = -(UTC_to_local);
 185   // Then we have to figure out if if we are ahead (+) or behind (-) UTC.
 186   char sign_local_to_UTC = '+';
 187   time_t abs_local_to_UTC = local_to_UTC;
 188   if (local_to_UTC < 0) {
 189     sign_local_to_UTC = '-';
 190     abs_local_to_UTC = -(abs_local_to_UTC);
 191   }
 192   // Convert time zone offset seconds to hours and minutes.
 193   const time_t seconds_per_minute = 60;
 194   const time_t minutes_per_hour = 60;
 195   const time_t seconds_per_hour = seconds_per_minute * minutes_per_hour;
 196   const time_t zone_hours = (abs_local_to_UTC / seconds_per_hour);
 197   const time_t zone_min =
 198     ((abs_local_to_UTC % seconds_per_hour) / seconds_per_minute);
 199 
 200   // Print an ISO 8601 date and time stamp into the buffer
 201   const int year = 1900 + time_struct.tm_year;
 202   const int month = 1 + time_struct.tm_mon;
 203   const int printed = jio_snprintf(buffer, buffer_length,
 204                                    "%04d-%02d-%02dT%02d:%02d:%02d.%03d%c%02d%02d",
 205                                    year,
 206                                    month,
 207                                    time_struct.tm_mday,
 208                                    time_struct.tm_hour,
 209                                    time_struct.tm_min,
 210                                    time_struct.tm_sec,
 211                                    milliseconds_after_second,
 212                                    sign_local_to_UTC,
 213                                    zone_hours,
 214                                    zone_min);
 215   if (printed == 0) {


< prev index next >