1 // Copyright 2006 Google Inc. All Rights Reserved.
2 // Author: nsanders, menderico
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
16 #ifndef STRESSAPPTEST_OS_H_ // NOLINT
17 #define STRESSAPPTEST_OS_H_
25 // This file must work with autoconf on its public version,
26 // so these includes are correct.
27 #include "adler32memcpy.h" // NOLINT
28 #include "sattypes.h" // NOLINT
30 const char kSysfsPath[] = "/sys/bus/pci/devices";
43 typedef vector<PCIDevice*> PCIDevices;
47 // This class implements OS/Platform specific funtions.
53 // Set the minimum amount of hugepages that should be available for testing.
54 // Must be set before Initialize().
55 void SetMinimumHugepagesSize(int64 min_bytes) {
56 min_hugepages_bytes_ = min_bytes;
59 // Initializes data strctures and open files.
60 // Returns false on error.
61 virtual bool Initialize();
63 // Virtual to physical. This implementation is optional for
64 // subclasses to implement.
65 // Takes a pointer, and returns the corresponding bus address.
66 virtual uint64 VirtualToPhysical(void *vaddr);
68 // Prints failed dimm. This implementation is optional for
69 // subclasses to implement.
70 // Takes a bus address and string, and prints the DIMM name
71 // into the string. Returns error status.
72 virtual int FindDimm(uint64 addr, char *buf, int len);
73 // Print dimm info, plus more available info.
74 virtual int FindDimmExtended(uint64 addr, char *buf, int len) {
75 return FindDimm(addr, buf, len);
79 // Classifies addresses according to "regions"
80 // This may mean different things on different platforms.
81 virtual int32 FindRegion(uint64 paddr);
82 // Find cpu cores associated with a region. Either NUMA or arbitrary.
83 virtual cpu_set_t *FindCoreMask(int32 region);
84 // Return cpu cores associated with a region in a hex string.
85 virtual string FindCoreMaskFormat(int32 region);
87 // Returns the HD device that contains this file.
88 virtual string FindFileDevice(string filename);
90 // Returns a list of paths coresponding to HD devices found on this machine.
91 virtual list<string> FindFileDevices();
93 // Polls for errors. This implementation is optional.
94 // This will poll once for errors and return zero iff no errors were found.
95 virtual int ErrorPoll();
97 // Delay an appropriate amount of time between polling.
98 virtual void ErrorWait();
100 // Report errors. This implementation is mandatory.
101 // This will output a machine readable line regarding the error.
102 virtual bool ErrorReport(const char *part, const char *symptom, int count);
104 // Flushes cacheline. Used to distinguish read or write errors.
105 // Subclasses may implement this in machine specific ways..
106 // Takes a pointer, and flushed the cacheline containing that pointer.
107 virtual void Flush(void *vaddr);
109 // Fast flush, for use in performance critical code.
110 // This is bound at compile time, and will not pick up
111 // any runtime machine configuration info.
112 inline static void FastFlush(void *vaddr) {
113 #ifdef STRESSAPPTEST_CPU_PPC
114 asm volatile("dcbf 0,%0; sync" : : "r" (vaddr));
115 #elif defined(STRESSAPPTEST_CPU_X86_64) || defined(STRESSAPPTEST_CPU_I686)
116 // Put mfence before and after clflush to make sure:
117 // 1. The write before the clflush is committed to memory bus;
118 // 2. The read after the clflush is hitting the memory bus.
120 // From Intel manual:
121 // CLFLUSH is only ordered by the MFENCE instruction. It is not guaranteed
122 // to be ordered by any other fencing, serializing or other CLFLUSH
123 // instruction. For example, software can use an MFENCE instruction to
124 // insure that previous stores are included in the write-back.
125 asm volatile("mfence");
126 asm volatile("clflush (%0)" :: "r" (vaddr));
127 asm volatile("mfence");
128 #elif defined(STRESSAPPTEST_CPU_ARMV7A)
129 #warning "Unsupported CPU type ARMV7A: Unable to force cache flushes."
131 #warning "Unsupported CPU type: Unable to force cache flushes."
135 // Get time in cpu timer ticks. Useful for matching MCEs with software
137 inline static uint64 GetTimestamp(void) {
139 #ifdef STRESSAPPTEST_CPU_PPC
140 uint32 tbl, tbu, temp;
148 : "=r"(tbl), "=r"(tbu), "=r"(temp)
152 tsc = (static_cast<uint64>(tbu) << 32) | static_cast<uint64>(tbl);
153 #elif defined(STRESSAPPTEST_CPU_X86_64) || defined(STRESSAPPTEST_CPU_I686)
155 __asm __volatile("rdtsc" : "=a" (data.l32.l), "=d"(data.l32.h));
157 #elif defined(STRESSAPPTEST_CPU_ARMV7A)
158 #warning "Unsupported CPU type ARMV7A: your build may not function correctly"
161 #warning "Unsupported CPU type: your build may not function correctly"
167 // Find the free memory on the machine.
168 virtual int64 FindFreeMemSize();
170 // Allocates test memory of length bytes.
171 // Subclasses must implement this.
172 // Call PepareTestMem to get a pointer.
173 virtual int64 AllocateAllMem(); // Returns length.
175 virtual bool AllocateTestMem(int64 length, uint64 paddr_base);
176 virtual void FreeTestMem();
178 // Prepares the memory for use. You must call this
179 // before using test memory, and after you are done.
180 virtual void *PrepareTestMem(uint64 offset, uint64 length);
181 virtual void ReleaseTestMem(void *addr, uint64 offset, uint64 length);
183 // Machine type detected. Can we implement all these functions correctly?
184 // Returns true if machine type is detected and implemented.
185 virtual bool IsSupported();
187 // Returns 32 for 32-bit, 64 for 64-bit.
188 virtual int AddressMode();
189 // Update OsLayer state regarding cpu support for various features.
190 virtual void GetFeatures();
192 // Open, read, write pci cfg through /proc/bus/pci. fd is /proc/pci file.
193 virtual int PciOpen(int bus, int device, int function);
194 virtual void PciWrite(int fd, uint32 offset, uint32 value, int width);
195 virtual uint32 PciRead(int fd, uint32 offset, int width);
198 virtual bool ReadMSR(uint32 core, uint32 address, uint64 *data);
199 virtual bool WriteMSR(uint32 core, uint32 address, uint64 *data);
201 // Extract bits [n+len-1, n] from a 32 bit word.
202 // so GetBitField(0x0f00, 8, 4) == 0xf.
203 virtual uint32 GetBitField(uint32 val, uint32 n, uint32 len);
205 // Platform and CPU specific CPU-stressing function.
206 // Returns true on success, false otherwise.
207 virtual bool CpuStressWorkload();
209 // Causes false errors for unittesting.
210 // Setting to "true" causes errors to be injected.
211 void set_error_injection(bool errors) { error_injection_ = errors; }
212 bool error_injection() const { return error_injection_; }
214 // Is SAT using normal malloc'd memory, or exotic mmap'd memory.
215 bool normal_mem() const { return normal_mem_; }
217 // Get numa config, if available..
218 int num_nodes() const { return num_nodes_; }
219 int num_cpus() const { return num_cpus_; }
221 // Handle to platform-specific error diagnoser.
222 ErrorDiag *error_diagnoser_;
224 // Detect all PCI Devices.
225 virtual PCIDevices GetPCIDevices();
227 // Disambiguate between different "warm" memcopies.
228 virtual bool AdlerMemcpyWarm(uint64 *dstmem, uint64 *srcmem,
229 unsigned int size_in_bytes,
230 AdlerChecksum *checksum);
232 // Store a callback to use to print
233 // app-specific info about the last error location.
234 // This call back is called with a physical address, and the app can fill in
235 // the most recent transaction that occurred at that address.
236 typedef bool (*ErrCallback)(uint64 paddr, string *buf);
237 void set_err_log_callback(
238 ErrCallback err_log_callback) {
239 err_log_callback_ = err_log_callback;
241 ErrCallback get_err_log_callback() { return err_log_callback_; }
244 void *testmem_; // Location of test memory.
245 uint64 testmemsize_; // Size of test memory.
246 int64 totalmemsize_; // Size of available memory.
247 int64 min_hugepages_bytes_; // Minimum hugepages size.
248 bool error_injection_; // Do error injection?
249 bool normal_mem_; // Memory DMA capable?
250 bool use_hugepages_; // Use hugepage shmem?
251 bool use_posix_shm_; // Use 4k page shmem?
252 bool dynamic_mapped_shmem_; // Conserve virtual address space.
253 int shmid_; // Handle to shmem
255 int64 regionsize_; // Size of memory "regions"
256 int regioncount_; // Number of memory "regions"
257 int num_cpus_; // Number of cpus in the system.
258 int num_nodes_; // Number of nodes in the system.
259 int num_cpus_per_node_; // Number of cpus per node in the system.
260 int address_mode_; // Are we running 32 or 64 bit?
261 bool has_sse2_; // Do we have sse2 instructions?
262 bool has_clflush_; // Do we have clflush instructions?
265 time_t time_initialized_; // Start time of test.
267 vector<cpu_set_t> cpu_sets_; // Cache for cpu masks.
268 vector<bool> cpu_sets_valid_; // If the cpu mask cache is valid.
270 // Get file descriptor for dev msr.
271 virtual int OpenMSR(uint32 core, uint32 address);
272 // Auxiliary methods for PCI device configuration
273 int PCIGetValue(string name, string object);
274 int PCIGetResources(string name, PCIDevice *device);
276 // Look up how many hugepages there are.
277 virtual int64 FindHugePages();
279 // Link to find last transaction at an error location.
280 ErrCallback err_log_callback_;
283 DISALLOW_COPY_AND_ASSIGN(OsLayer);
286 // Selects and returns the proper OS and hardware interface. Does not call
287 // OsLayer::Initialize() on the new object.
288 OsLayer *OsLayerFactory(const std::map<std::string, std::string> &options);
290 #endif // STRESSAPPTEST_OS_H_ NOLINT