1// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#ifndef RUNTIME_VM_OS_H_
6#define RUNTIME_VM_OS_H_
7
8#include "vm/globals.h"
9
10// Forward declarations.
11struct tm;
12
13namespace dart {
14
15// Forward declarations.
16class Zone;
17
18// Interface to the underlying OS platform.
19class OS {
20 public:
21 // Returns the name of the given OS. For example "linux".
22 static const char* Name();
23
24 // Returns the current process id.
25 static intptr_t ProcessId();
26
27 // Returns a time-zone name for the given instant.
28 // The name is provided by the underlying platform.
29 // The returned string may be Zone allocated.
30 static const char* GetTimeZoneName(int64_t seconds_since_epoch);
31
32 // Returns the difference in seconds between local time and UTC for the given
33 // instant.
34 // For example 3600 for CET, and 7200 for CEST.
35 static int GetTimeZoneOffsetInSeconds(int64_t seconds_since_epoch);
36
37 // Returns the difference in seconds between local time and UTC when no
38 // daylight saving is active.
39 // For example 3600 in CET and CEST.
40 static int GetLocalTimeZoneAdjustmentInSeconds();
41
42 // Returns the current time in milliseconds measured
43 // from midnight January 1, 1970 UTC.
44 static int64_t GetCurrentTimeMillis();
45
46 // Returns the current time in microseconds measured
47 // from midnight January 1, 1970 UTC.
48 static int64_t GetCurrentTimeMicros();
49
50 // Returns the current time used by the tracing infrastructure.
51 static int64_t GetCurrentMonotonicMicros();
52
53 // Returns the raw clock value from the monotonic clock.
54 static int64_t GetCurrentMonotonicTicks();
55
56 // Returns the frequency of the monotonic clock.
57 static int64_t GetCurrentMonotonicFrequency();
58
59 // Returns the value of current thread's CPU usage clock in microseconds.
60 // NOTE: This clock will return different values depending on the calling
61 // thread. It is only expected to increase in value as the thread uses
62 // CPU time.
63 // NOTE: This function will return -1 on OSs that are not supported.
64 static int64_t GetCurrentThreadCPUMicros();
65
66 // If the tracing/timeline configuration on the current OS supports thread
67 // timestamps, returns the same value as |GetCurrentThreadCPUMicros|.
68 // Otherwise, returns -1.
69 static int64_t GetCurrentThreadCPUMicrosForTimeline();
70
71 // Returns the activation frame alignment constraint or one if
72 // the platform doesn't care. Guaranteed to be a power of two.
73 static intptr_t ActivationFrameAlignment();
74
75 // Returns number of available processor cores.
76 static int NumberOfAvailableProcessors();
77
78 // Sleep the currently executing thread for millis ms.
79 static void Sleep(int64_t millis);
80
81 // Sleep the currently executing thread for micros microseconds.
82 static void SleepMicros(int64_t micros);
83
84 // Debug break.
85 static void DebugBreak();
86
87 // Returns the current program counter.
88 static uintptr_t GetProgramCounter();
89
90 // Print formatted output to stdout/stderr for debugging.
91 // Tracing and debugging prints from the VM should strongly prefer to use
92 // PrintErr to avoid interfering with the application's output, which may
93 // be parsed by another program.
94 static void Print(const char* format, ...) PRINTF_ATTRIBUTE(1, 2);
95 static void PrintErr(const char* format, ...) PRINTF_ATTRIBUTE(1, 2);
96 static void VFPrint(FILE* stream, const char* format, va_list args);
97
98 // Allocate a string and print formatted output into the buffer.
99 // Uses the zone for allocation if one if provided, and otherwise uses
100 // malloc.
101 static char* SCreate(Zone* zone, const char* format, ...)
102 PRINTF_ATTRIBUTE(2, 3);
103 static char* VSCreate(Zone* zone, const char* format, va_list args);
104
105 // Converts a C string which represents a valid dart integer into a 64 bit
106 // value.
107 // Returns false if it is unable to convert the string to a 64 bit value,
108 // the failure could be because of underflow/overflow or invalid characters.
109 // On success the function returns true and 'value' contains the converted
110 // value.
111 static bool StringToInt64(const char* str, int64_t* value);
112
113 // Register code observers relevant to this OS.
114 static void RegisterCodeObservers();
115
116 // Initialize the OS class.
117 static void Init();
118
119 // Cleanup the OS class.
120 static void Cleanup();
121
122 // Only implemented on Windows, prevents cleanup code from running.
123 static void PrepareToAbort();
124
125 DART_NORETURN static void Abort();
126
127 DART_NORETURN static void Exit(int code);
128};
129
130} // namespace dart
131
132#endif // RUNTIME_VM_OS_H_
133