chiark / gitweb /
New frequency test, fixed error accounting, added logging timestamps, and miscellaneo...
[stressapptest] / src / sat.cc
1 // Copyright 2006 Google Inc. All Rights Reserved.
2
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6
7 //      http://www.apache.org/licenses/LICENSE-2.0
8
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // sat.cc : a stress test for stressful testing
16
17 // stressapptest (or SAT, from Stressful Application Test) is a test
18 // designed to stress the system, as well as provide a comprehensive
19 // memory interface test.
20
21 // stressapptest can be run using memory only, or using many system components.
22
23 #include <errno.h>
24 #include <pthread.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include <sys/stat.h>
33 #include <sys/times.h>
34
35 // #define __USE_GNU
36 // #define __USE_LARGEFILE64
37 #include <fcntl.h>
38
39 #include <list>
40 #include <string>
41
42 // This file must work with autoconf on its public version,
43 // so these includes are correct.
44 #include "disk_blocks.h"
45 #include "logger.h"
46 #include "os.h"
47 #include "sat.h"
48 #include "sattypes.h"
49 #include "worker.h"
50
51 // stressapptest versioning here.
52 #ifndef PACKAGE_VERSION
53 static const char* kVersion = "1.0.0";
54 #else
55 static const char* kVersion = PACKAGE_VERSION;
56 #endif
57
58 // Global stressapptest reference, for use by signal handler.
59 // This makes Sat objects not safe for multiple instances.
60 namespace {
61   Sat *g_sat = NULL;
62
63   // Signal handler for catching break or kill.
64   //
65   // This must be installed after g_sat is assigned and while there is a single
66   // thread.
67   //
68   // This must be uninstalled while there is only a single thread, and of course
69   // before g_sat is cleared or deleted.
70   void SatHandleBreak(int signal) {
71     g_sat->Break();
72   }
73 }
74
75 // Opens the logfile for writing if necessary
76 bool Sat::InitializeLogfile() {
77   // Open logfile.
78   if (use_logfile_) {
79     logfile_ = open(logfilename_,
80 #if defined(O_DSYNC)
81                     O_DSYNC |
82 #elif defined(O_SYNC)
83                     O_SYNC |
84 #elif defined(O_FSYNC)
85                     O_FSYNC |
86 #endif
87                     O_WRONLY | O_CREAT,
88                     S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
89     if (logfile_ < 0) {
90       printf("Fatal Error: cannot open file %s for logging\n",
91              logfilename_);
92       bad_status();
93       return false;
94     }
95     // We seek to the end once instead of opening in append mode because no
96     // other processes should be writing to it while this one exists.
97     if (lseek(logfile_, 0, SEEK_END) == -1) {
98       printf("Fatal Error: cannot seek to end of logfile (%s)\n",
99              logfilename_);
100       bad_status();
101       return false;
102     }
103     Logger::GlobalLogger()->SetLogFd(logfile_);
104   }
105   return true;
106 }
107
108 // Check that the environment is known and safe to run on.
109 // Return 1 if good, 0 if unsuppported.
110 bool Sat::CheckEnvironment() {
111   // Check that this is not a debug build. Debug builds lack
112   // enough performance to stress the system.
113 #if !defined NDEBUG
114   if (run_on_anything_) {
115     logprintf(1, "Log: Running DEBUG version of SAT, "
116                  "with significantly reduced coverage.\n");
117   } else {
118     logprintf(0, "Process Error: Running DEBUG version of SAT, "
119                  "with significantly reduced coverage.\n");
120     logprintf(0, "Log: Command line option '-A' bypasses this error.\n");
121     bad_status();
122     return false;
123   }
124 #elif !defined CHECKOPTS
125   #error Build system regression - COPTS disregarded.
126 #endif
127
128   // Check if the cpu frequency test is enabled and able to run.
129   if (cpu_freq_test_) {
130     if (!CpuFreqThread::CanRun()) {
131       logprintf(0, "Process Error: This platform does not support this "
132                 "test.\n");
133       bad_status();
134       return false;
135     } else if (cpu_freq_threshold_ <= 0) {
136       logprintf(0, "Process Error: The cpu frequency test requires "
137                 "--cpu_freq_threshold set to a value > 0\n");
138       bad_status();
139       return false;
140     } else if (cpu_freq_round_ < 0) {
141       logprintf(0, "Process Error: The --cpu_freq_round option must be greater"
142                 " than or equal to zero. A value of zero means no rounding.\n");
143       bad_status();
144       return false;
145     }
146   }
147
148   // Use all CPUs if nothing is specified.
149   if (memory_threads_ == -1) {
150     memory_threads_ = os_->num_cpus();
151     logprintf(7, "Log: Defaulting to %d copy threads\n", memory_threads_);
152   }
153
154   // Use all memory if no size is specified.
155   if (size_mb_ == 0)
156     size_mb_ = os_->FindFreeMemSize() / kMegabyte;
157   size_ = static_cast<int64>(size_mb_) * kMegabyte;
158
159   // Autodetect file locations.
160   if (findfiles_ && (file_threads_ == 0)) {
161     // Get a space separated sting of disk locations.
162     list<string> locations = os_->FindFileDevices();
163
164     // Extract each one.
165     while (!locations.empty()) {
166       // Copy and remove the disk name.
167       string disk = locations.back();
168       locations.pop_back();
169
170       logprintf(12, "Log: disk at %s\n", disk.c_str());
171       file_threads_++;
172       filename_.push_back(disk + "/sat_disk.a");
173       file_threads_++;
174       filename_.push_back(disk + "/sat_disk.b");
175     }
176   }
177
178   // We'd better have some memory by this point.
179   if (size_ < 1) {
180     logprintf(0, "Process Error: No memory found to test.\n");
181     bad_status();
182     return false;
183   }
184
185   if (tag_mode_ && ((file_threads_ > 0) ||
186                     (disk_threads_ > 0) ||
187                     (net_threads_ > 0))) {
188     logprintf(0, "Process Error: Memory tag mode incompatible "
189                  "with disk/network DMA.\n");
190     bad_status();
191     return false;
192   }
193
194   // If platform is 32 bit Xeon, floor memory size to multiple of 4.
195   if (address_mode_ == 32) {
196     size_mb_ = (size_mb_ / 4) * 4;
197     size_ = size_mb_ * kMegabyte;
198     logprintf(1, "Log: Flooring memory allocation to multiple of 4: %lldMB\n",
199               size_mb_);
200   }
201
202   // Check if this system is on the whitelist for supported systems.
203   if (!os_->IsSupported()) {
204     if (run_on_anything_) {
205       logprintf(1, "Log: Unsupported system. Running with reduced coverage.\n");
206       // This is ok, continue on.
207     } else {
208       logprintf(0, "Process Error: Unsupported system, "
209                    "no error reporting available\n");
210       logprintf(0, "Log: Command line option '-A' bypasses this error.\n");
211       bad_status();
212       return false;
213     }
214   }
215
216   return true;
217 }
218
219 // Allocates memory to run the test on
220 bool Sat::AllocateMemory() {
221   // Allocate our test memory.
222   bool result = os_->AllocateTestMem(size_, paddr_base_);
223   if (!result) {
224     logprintf(0, "Process Error: failed to allocate memory\n");
225     bad_status();
226     return false;
227   }
228   return true;
229 }
230
231 // Sets up access to data patterns
232 bool Sat::InitializePatterns() {
233   // Initialize pattern data.
234   patternlist_ = new PatternList();
235   if (!patternlist_) {
236     logprintf(0, "Process Error: failed to allocate patterns\n");
237     bad_status();
238     return false;
239   }
240   if (!patternlist_->Initialize()) {
241     logprintf(0, "Process Error: failed to initialize patternlist\n");
242     bad_status();
243     return false;
244   }
245   return true;
246 }
247
248 // Get any valid page, no tag specified.
249 bool Sat::GetValid(struct page_entry *pe) {
250   return GetValid(pe, kDontCareTag);
251 }
252
253
254 // Fetch and return empty and full pages into the empty and full pools.
255 bool Sat::GetValid(struct page_entry *pe, int32 tag) {
256   bool result = false;
257   // Get valid page depending on implementation.
258   if (pe_q_implementation_ == SAT_FINELOCK)
259     result = finelock_q_->GetValid(pe, tag);
260   else if (pe_q_implementation_ == SAT_ONELOCK)
261     result = valid_->PopRandom(pe);
262
263   if (result) {
264     pe->addr = os_->PrepareTestMem(pe->offset, page_length_);  // Map it.
265
266     // Tag this access and current pattern.
267     pe->ts = os_->GetTimestamp();
268     pe->lastpattern = pe->pattern;
269
270     return (pe->addr != 0);     // Return success or failure.
271   }
272   return false;
273 }
274
275 bool Sat::PutValid(struct page_entry *pe) {
276   if (pe->addr != 0)
277     os_->ReleaseTestMem(pe->addr, pe->offset, page_length_);  // Unmap the page.
278   pe->addr = 0;
279
280   // Put valid page depending on implementation.
281   if (pe_q_implementation_ == SAT_FINELOCK)
282     return finelock_q_->PutValid(pe);
283   else if (pe_q_implementation_ == SAT_ONELOCK)
284     return valid_->Push(pe);
285   else
286     return false;
287 }
288
289 // Get an empty page with any tag.
290 bool Sat::GetEmpty(struct page_entry *pe) {
291   return GetEmpty(pe, kDontCareTag);
292 }
293
294 bool Sat::GetEmpty(struct page_entry *pe, int32 tag) {
295   bool result = false;
296   // Get empty page depending on implementation.
297   if (pe_q_implementation_ == SAT_FINELOCK)
298     result = finelock_q_->GetEmpty(pe, tag);
299   else if (pe_q_implementation_ == SAT_ONELOCK)
300     result = empty_->PopRandom(pe);
301
302   if (result) {
303     pe->addr = os_->PrepareTestMem(pe->offset, page_length_);  // Map it.
304     return (pe->addr != 0);     // Return success or failure.
305   }
306   return false;
307 }
308
309 bool Sat::PutEmpty(struct page_entry *pe) {
310   if (pe->addr != 0)
311     os_->ReleaseTestMem(pe->addr, pe->offset, page_length_);  // Unmap the page.
312   pe->addr = 0;
313
314   // Put empty page depending on implementation.
315   if (pe_q_implementation_ == SAT_FINELOCK)
316     return finelock_q_->PutEmpty(pe);
317   else if (pe_q_implementation_ == SAT_ONELOCK)
318     return empty_->Push(pe);
319   else
320     return false;
321 }
322
323 // Set up the bitmap of physical pages in case we want to see which pages were
324 // accessed under this run of SAT.
325 void Sat::AddrMapInit() {
326   if (!do_page_map_)
327     return;
328   // Find about how much physical mem is in the system.
329   // TODO(nsanders): Find some way to get the max
330   // and min phys addr in the system.
331   uint64 maxsize = os_->FindFreeMemSize() * 4;
332   sat_assert(maxsize != 0);
333
334   // Make a bitmask of this many pages. Assume that the memory is relatively
335   // zero based. This is true on x86, typically.
336   // This is one bit per page.
337   uint64 arraysize = maxsize / 4096 / 8;
338   unsigned char *bitmap = new unsigned char[arraysize];
339   sat_assert(bitmap);
340
341   // Mark every page as 0, not seen.
342   memset(bitmap, 0, arraysize);
343
344   page_bitmap_size_ = maxsize;
345   page_bitmap_ = bitmap;
346 }
347
348 // Add the 4k pages in this block to the array of pages SAT has seen.
349 void Sat::AddrMapUpdate(struct page_entry *pe) {
350   if (!do_page_map_)
351     return;
352
353   // Go through 4k page blocks.
354   uint64 arraysize = page_bitmap_size_ / 4096 / 8;
355
356   char *base = reinterpret_cast<char*>(pe->addr);
357   for (int i = 0; i < page_length_; i += 4096) {
358     uint64 paddr = os_->VirtualToPhysical(base + i);
359
360     uint32 offset = paddr / 4096 / 8;
361     unsigned char mask = 1 << ((paddr / 4096) % 8);
362
363     if (offset >= arraysize) {
364       logprintf(0, "Process Error: Physical address %#llx is "
365                    "greater than expected %#llx.\n",
366                 paddr, page_bitmap_size_);
367       sat_assert(0);
368     }
369     page_bitmap_[offset] |= mask;
370   }
371 }
372
373 // Print out the physical memory ranges that SAT has accessed.
374 void Sat::AddrMapPrint() {
375   if (!do_page_map_)
376     return;
377
378   uint64 pages = page_bitmap_size_ / 4096;
379
380   uint64 last_page = 0;
381   bool valid_range = false;
382
383   logprintf(4, "Log: Printing tested physical ranges.\n");
384
385   for (uint64 i = 0; i < pages; i ++) {
386     int offset = i / 8;
387     unsigned char mask = 1 << (i % 8);
388
389     bool touched = page_bitmap_[offset] & mask;
390     if (touched && !valid_range) {
391       valid_range = true;
392       last_page = i * 4096;
393     } else if (!touched && valid_range) {
394       valid_range = false;
395       logprintf(4, "Log: %#016llx - %#016llx\n", last_page, (i * 4096) - 1);
396     }
397   }
398   logprintf(4, "Log: Done printing physical ranges.\n");
399 }
400
401 // Initializes page lists and fills pages with data patterns.
402 bool Sat::InitializePages() {
403   int result = 1;
404   // Calculate needed page totals.
405   int64 neededpages = memory_threads_ +
406     invert_threads_ +
407     check_threads_ +
408     net_threads_ +
409     file_threads_;
410
411   // Empty-valid page ratio is adjusted depending on queue implementation.
412   // since fine-grain-locked queue keeps both valid and empty entries in the
413   // same queue and randomly traverse to find pages, the empty-valid ratio
414   // should be more even.
415   if (pe_q_implementation_ == SAT_FINELOCK)
416     freepages_ = pages_ / 5 * 2;  // Mark roughly 2/5 of all pages as Empty.
417   else
418     freepages_ = (pages_ / 100) + (2 * neededpages);
419
420   if (freepages_ < neededpages) {
421     logprintf(0, "Process Error: freepages < neededpages.\n");
422     logprintf(1, "Stats: Total: %lld, Needed: %lld, Marked free: %lld\n",
423               static_cast<int64>(pages_),
424               static_cast<int64>(neededpages),
425               static_cast<int64>(freepages_));
426     bad_status();
427     return false;
428   }
429
430   if (freepages_ >  pages_/2) {
431     logprintf(0, "Process Error: not enough pages for IO\n");
432     logprintf(1, "Stats: Total: %lld, Needed: %lld, Available: %lld\n",
433               static_cast<int64>(pages_),
434               static_cast<int64>(freepages_),
435               static_cast<int64>(pages_/2));
436     bad_status();
437     return false;
438   }
439   logprintf(12, "Log: Allocating pages, Total: %lld Free: %lld\n",
440             pages_,
441             freepages_);
442
443   // Initialize page locations.
444   for (int64 i = 0; i < pages_; i++) {
445     struct page_entry pe;
446     init_pe(&pe);
447     pe.offset = i * page_length_;
448     result &= PutEmpty(&pe);
449   }
450
451   if (!result) {
452     logprintf(0, "Process Error: while initializing empty_ list\n");
453     bad_status();
454     return false;
455   }
456
457   // Fill valid pages with test patterns.
458   // Use fill threads to do this.
459   WorkerStatus fill_status;
460   WorkerVector fill_vector;
461
462   logprintf(12, "Starting Fill threads: %d threads, %d pages\n",
463             fill_threads_, pages_);
464   // Initialize the fill threads.
465   for (int i = 0; i < fill_threads_; i++) {
466     FillThread *thread = new FillThread();
467     thread->InitThread(i, this, os_, patternlist_, &fill_status);
468     if (i != fill_threads_ - 1) {
469         logprintf(12, "Starting Fill Threads %d: %d pages\n",
470                   i, pages_ / fill_threads_);
471         thread->SetFillPages(pages_ / fill_threads_);
472       // The last thread finishes up all the leftover pages.
473     } else {
474       logprintf(12, "Starting Fill Threads %d: %d pages\n",
475                 i, pages_ - pages_ / fill_threads_ * i);
476         thread->SetFillPages(pages_ - pages_ / fill_threads_ * i);
477     }
478     fill_vector.push_back(thread);
479   }
480
481   // Spawn the fill threads.
482   fill_status.Initialize();
483   for (WorkerVector::const_iterator it = fill_vector.begin();
484        it != fill_vector.end(); ++it)
485     (*it)->SpawnThread();
486
487   // Reap the finished fill threads.
488   for (WorkerVector::const_iterator it = fill_vector.begin();
489        it != fill_vector.end(); ++it) {
490     (*it)->JoinThread();
491     if ((*it)->GetStatus() != 1) {
492       logprintf(0, "Thread %d failed with status %d at %.2f seconds\n",
493                 (*it)->ThreadID(), (*it)->GetStatus(),
494                 (*it)->GetRunDurationUSec() * 1.0/1000000);
495       bad_status();
496       return false;
497     }
498     delete (*it);
499   }
500   fill_vector.clear();
501   fill_status.Destroy();
502   logprintf(12, "Log: Done filling pages.\n");
503   logprintf(12, "Log: Allocating pages.\n");
504
505   AddrMapInit();
506
507   // Initialize page locations.
508   for (int64 i = 0; i < pages_; i++) {
509     struct page_entry pe;
510     // Only get valid pages with uninitialized tags here.
511     if (GetValid(&pe, kInvalidTag)) {
512       int64 paddr = os_->VirtualToPhysical(pe.addr);
513       int32 region = os_->FindRegion(paddr);
514       region_[region]++;
515       pe.paddr = paddr;
516       pe.tag = 1 << region;
517       region_mask_ |= pe.tag;
518
519       // Generate a physical region map
520       AddrMapUpdate(&pe);
521
522       // Note: this does not allocate free pages among all regions
523       // fairly. However, with large enough (thousands) random number
524       // of pages being marked free in each region, the free pages
525       // count in each region end up pretty balanced.
526       if (i < freepages_) {
527         result &= PutEmpty(&pe);
528       } else {
529         result &= PutValid(&pe);
530       }
531     } else {
532       logprintf(0, "Log: didn't tag all pages. %d - %d = %d\n",
533                 pages_, i, pages_ - i);
534       return false;
535     }
536   }
537   logprintf(12, "Log: Done allocating pages.\n");
538
539   AddrMapPrint();
540
541   for (int i = 0; i < 32; i++) {
542     if (region_mask_ & (1 << i)) {
543       region_count_++;
544       logprintf(12, "Log: Region %d: %d.\n", i, region_[i]);
545     }
546   }
547   logprintf(5, "Log: Region mask: 0x%x\n", region_mask_);
548
549   return true;
550 }
551
552 // Print SAT version info.
553 bool Sat::PrintVersion() {
554   logprintf(1, "Stats: SAT revision %s, %d bit binary\n",
555             kVersion, address_mode_);
556   logprintf(5, "Log: %s from %s\n", Timestamp(), BuildChangelist());
557
558   return true;
559 }
560
561
562 // Initializes the resources that SAT needs to run.
563 // This needs to be called before Run(), and after ParseArgs().
564 // Returns true on success, false on error, and will exit() on help message.
565 bool Sat::Initialize() {
566   g_sat = this;
567
568   // Initializes sync'd log file to ensure output is saved.
569   if (!InitializeLogfile())
570     return false;
571   Logger::GlobalLogger()->SetTimestampLogging(log_timestamps_);
572   Logger::GlobalLogger()->StartThread();
573
574   logprintf(5, "Log: Commandline - %s\n", cmdline_.c_str());
575   PrintVersion();
576
577   std::map<std::string, std::string> options;
578
579   GoogleOsOptions(&options);
580
581   // Initialize OS/Hardware interface.
582   os_ = OsLayerFactory(options);
583   if (!os_) {
584     bad_status();
585     return false;
586   }
587
588   if (min_hugepages_mbytes_ > 0)
589     os_->SetMinimumHugepagesSize(min_hugepages_mbytes_ * kMegabyte);
590
591   if (reserve_mb_ > 0)
592     os_->SetReserveSize(reserve_mb_);
593
594   if (channels_.size() > 0) {
595     logprintf(6, "Log: Decoding memory: %dx%d bit channels,"
596         "%d modules per channel (x%d), decoding hash 0x%x\n",
597         channels_.size(), channel_width_, channels_[0].size(),
598         channel_width_/channels_[0].size(), channel_hash_);
599     os_->SetDramMappingParams(channel_hash_, channel_width_, &channels_);
600   }
601
602   if (!os_->Initialize()) {
603     logprintf(0, "Process Error: Failed to initialize OS layer\n");
604     bad_status();
605     delete os_;
606     return false;
607   }
608
609   // Checks that OS/Build/Platform is supported.
610   if (!CheckEnvironment())
611     return false;
612
613   if (error_injection_)
614     os_->set_error_injection(true);
615
616   // Run SAT in monitor only mode, do not continue to allocate resources.
617   if (monitor_mode_) {
618     logprintf(5, "Log: Running in monitor-only mode. "
619                  "Will not allocate any memory nor run any stress test. "
620                  "Only polling ECC errors.\n");
621     return true;
622   }
623
624   // Allocate the memory to test.
625   if (!AllocateMemory())
626     return false;
627
628   logprintf(5, "Stats: Starting SAT, %dM, %d seconds\n",
629             static_cast<int>(size_/kMegabyte),
630             runtime_seconds_);
631
632   if (!InitializePatterns())
633     return false;
634
635   // Initialize memory allocation.
636   pages_ = size_ / page_length_;
637
638   // Allocate page queue depending on queue implementation switch.
639   if (pe_q_implementation_ == SAT_FINELOCK) {
640       finelock_q_ = new FineLockPEQueue(pages_, page_length_);
641       if (finelock_q_ == NULL)
642         return false;
643       finelock_q_->set_os(os_);
644       os_->set_err_log_callback(finelock_q_->get_err_log_callback());
645   } else if (pe_q_implementation_ == SAT_ONELOCK) {
646       empty_ = new PageEntryQueue(pages_);
647       valid_ = new PageEntryQueue(pages_);
648       if ((empty_ == NULL) || (valid_ == NULL))
649         return false;
650   }
651
652   if (!InitializePages()) {
653     logprintf(0, "Process Error: Initialize Pages failed\n");
654     return false;
655   }
656
657   return true;
658 }
659
660 // Constructor and destructor.
661 Sat::Sat() {
662   // Set defaults, command line might override these.
663   runtime_seconds_ = 20;
664   page_length_ = kSatPageSize;
665   disk_pages_ = kSatDiskPage;
666   pages_ = 0;
667   size_mb_ = 0;
668   size_ = size_mb_ * kMegabyte;
669   reserve_mb_ = 0;
670   min_hugepages_mbytes_ = 0;
671   freepages_ = 0;
672   paddr_base_ = 0;
673   channel_hash_ = kCacheLineSize;
674   channel_width_ = 64;
675
676   user_break_ = false;
677   verbosity_ = 8;
678   Logger::GlobalLogger()->SetVerbosity(verbosity_);
679   strict_ = 1;
680   warm_ = 0;
681   run_on_anything_ = 0;
682   use_logfile_ = 0;
683   logfile_ = 0;
684   log_timestamps_ = true;
685   // Detect 32/64 bit binary.
686   void *pvoid = 0;
687   address_mode_ = sizeof(pvoid) * 8;
688   error_injection_ = false;
689   crazy_error_injection_ = false;
690   max_errorcount_ = 0;  // Zero means no early exit.
691   stop_on_error_ = false;
692   error_poll_ = true;
693   findfiles_ = false;
694
695   do_page_map_ = false;
696   page_bitmap_ = 0;
697   page_bitmap_size_ = 0;
698
699   // Cache coherency data initialization.
700   cc_test_ = false;         // Flag to trigger cc threads.
701   cc_cacheline_count_ = 2;  // Two datastructures of cache line size.
702   cc_cacheline_size_ = 0;   // Size of a cacheline (0 for auto-detect).
703   cc_inc_count_ = 1000;     // Number of times to increment the shared variable.
704   cc_cacheline_data_ = 0;   // Cache Line size datastructure.
705
706   // Cpu frequency data initialization.
707   cpu_freq_test_ = false;   // Flag to trigger cpu frequency thread.
708   cpu_freq_threshold_ = 0;  // Threshold, in MHz, at which a cpu fails.
709   cpu_freq_round_ = 10;     // Round the computed frequency to this value.
710
711   sat_assert(0 == pthread_mutex_init(&worker_lock_, NULL));
712   file_threads_ = 0;
713   net_threads_ = 0;
714   listen_threads_ = 0;
715   // Default to autodetect number of cpus, and run that many threads.
716   memory_threads_ = -1;
717   invert_threads_ = 0;
718   fill_threads_ = 8;
719   check_threads_ = 0;
720   cpu_stress_threads_ = 0;
721   disk_threads_ = 0;
722   total_threads_ = 0;
723
724   region_mask_ = 0;
725   region_count_ = 0;
726   for (int i = 0; i < 32; i++) {
727     region_[i] = 0;
728   }
729   region_mode_ = 0;
730
731   errorcount_ = 0;
732   statuscount_ = 0;
733
734   valid_ = 0;
735   empty_ = 0;
736   finelock_q_ = 0;
737   // Default to use fine-grain lock for better performance.
738   pe_q_implementation_ = SAT_FINELOCK;
739
740   os_ = 0;
741   patternlist_ = 0;
742   logfilename_[0] = 0;
743
744   read_block_size_ = 512;
745   write_block_size_ = -1;
746   segment_size_ = -1;
747   cache_size_ = -1;
748   blocks_per_segment_ = -1;
749   read_threshold_ = -1;
750   write_threshold_ = -1;
751   non_destructive_ = 1;
752   monitor_mode_ = 0;
753   tag_mode_ = 0;
754   random_threads_ = 0;
755
756   pause_delay_ = 600;
757   pause_duration_ = 15;
758 }
759
760 // Destructor.
761 Sat::~Sat() {
762   // We need to have called Cleanup() at this point.
763   // We should probably enforce this.
764 }
765
766
767 #define ARG_KVALUE(argument, variable, value)         \
768   if (!strcmp(argv[i], argument)) {                   \
769     variable = value;                                 \
770     continue;                                         \
771   }
772
773 #define ARG_IVALUE(argument, variable)                \
774   if (!strcmp(argv[i], argument)) {                   \
775     i++;                                              \
776     if (i < argc)                                     \
777       variable = strtoull(argv[i], NULL, 0);          \
778     continue;                                         \
779   }
780
781 #define ARG_SVALUE(argument, variable)                     \
782   if (!strcmp(argv[i], argument)) {                        \
783     i++;                                                   \
784     if (i < argc)                                          \
785       snprintf(variable, sizeof(variable), "%s", argv[i]); \
786     continue;                                              \
787   }
788
789 // Configures SAT from command line arguments.
790 // This will call exit() given a request for
791 // self-documentation or unexpected args.
792 bool Sat::ParseArgs(int argc, char **argv) {
793   int i;
794   uint64 filesize = page_length_ * disk_pages_;
795
796   // Parse each argument.
797   for (i = 1; i < argc; i++) {
798     // Switch to fall back to corase-grain-lock queue. (for benchmarking)
799     ARG_KVALUE("--coarse_grain_lock", pe_q_implementation_, SAT_ONELOCK);
800
801     // Set number of megabyte to use.
802     ARG_IVALUE("-M", size_mb_);
803
804     // Specify the amount of megabytes to be reserved for system.
805     ARG_IVALUE("--reserve_memory", reserve_mb_);
806
807     // Set minimum megabytes of hugepages to require.
808     ARG_IVALUE("-H", min_hugepages_mbytes_);
809
810     // Set number of seconds to run.
811     ARG_IVALUE("-s", runtime_seconds_);
812
813     // Set number of memory copy threads.
814     ARG_IVALUE("-m", memory_threads_);
815
816     // Set number of memory invert threads.
817     ARG_IVALUE("-i", invert_threads_);
818
819     // Set number of check-only threads.
820     ARG_IVALUE("-c", check_threads_);
821
822     // Set number of cache line size datastructures.
823     ARG_IVALUE("--cc_inc_count", cc_inc_count_);
824
825     // Set number of cache line size datastructures
826     ARG_IVALUE("--cc_line_count", cc_cacheline_count_);
827
828     // Override the detected or assumed cache line size.
829     ARG_IVALUE("--cc_line_size", cc_cacheline_size_);
830
831     // Flag set when cache coherency tests need to be run
832     ARG_KVALUE("--cc_test", cc_test_, true);
833
834     // Set when the cpu_frequency test needs to be run
835     ARG_KVALUE("--cpu_freq_test", cpu_freq_test_, true);
836
837     // Set the threshold in MHz at which the cpu frequency test will fail.
838     ARG_IVALUE("--cpu_freq_threshold", cpu_freq_threshold_);
839
840     // Set the rounding value for the cpu frequency test. The default is to
841     // round to the nearest 10s value.
842     ARG_IVALUE("--cpu_freq_round", cpu_freq_round_);
843
844     // Set number of CPU stress threads.
845     ARG_IVALUE("-C", cpu_stress_threads_);
846
847     // Set logfile name.
848     ARG_SVALUE("-l", logfilename_);
849
850     // Verbosity level.
851     ARG_IVALUE("-v", verbosity_);
852
853     // Turn off timestamps logging.
854     ARG_KVALUE("--no_timestamps", log_timestamps_, false);
855
856     // Set maximum number of errors to collect. Stop running after this many.
857     ARG_IVALUE("--max_errors", max_errorcount_);
858
859     // Set pattern block size.
860     ARG_IVALUE("-p", page_length_);
861
862     // Set pattern block size.
863     ARG_IVALUE("--filesize", filesize);
864
865     // NUMA options.
866     ARG_KVALUE("--local_numa", region_mode_, kLocalNuma);
867     ARG_KVALUE("--remote_numa", region_mode_, kRemoteNuma);
868
869     // Autodetect tempfile locations.
870     ARG_KVALUE("--findfiles", findfiles_, 1);
871
872     // Inject errors to force miscompare code paths
873     ARG_KVALUE("--force_errors", error_injection_, true);
874     ARG_KVALUE("--force_errors_like_crazy", crazy_error_injection_, true);
875     if (crazy_error_injection_)
876       error_injection_ = true;
877
878     // Stop immediately on any arror, for debugging HW problems.
879     ARG_KVALUE("--stop_on_errors", stop_on_error_, 1);
880
881     // Don't use internal error polling, allow external detection.
882     ARG_KVALUE("--no_errors", error_poll_, 0);
883
884     // Never check data as you go.
885     ARG_KVALUE("-F", strict_, 0);
886
887     // Warm the cpu as you go.
888     ARG_KVALUE("-W", warm_, 1);
889
890     // Allow runnign on unknown systems with base unimplemented OsLayer
891     ARG_KVALUE("-A", run_on_anything_, 1);
892
893     // Size of read blocks for disk test.
894     ARG_IVALUE("--read-block-size", read_block_size_);
895
896     // Size of write blocks for disk test.
897     ARG_IVALUE("--write-block-size", write_block_size_);
898
899     // Size of segment for disk test.
900     ARG_IVALUE("--segment-size", segment_size_);
901
902     // Size of disk cache size for disk test.
903     ARG_IVALUE("--cache-size", cache_size_);
904
905     // Number of blocks to test per segment.
906     ARG_IVALUE("--blocks-per-segment", blocks_per_segment_);
907
908     // Maximum time a block read should take before warning.
909     ARG_IVALUE("--read-threshold", read_threshold_);
910
911     // Maximum time a block write should take before warning.
912     ARG_IVALUE("--write-threshold", write_threshold_);
913
914     // Do not write anything to disk in the disk test.
915     ARG_KVALUE("--destructive", non_destructive_, 0);
916
917     // Run SAT in monitor mode. No test load at all.
918     ARG_KVALUE("--monitor_mode", monitor_mode_, true);
919
920     // Run SAT in address mode. Tag all cachelines by virt addr.
921     ARG_KVALUE("--tag_mode", tag_mode_, true);
922
923     // Dump range map of tested pages..
924     ARG_KVALUE("--do_page_map", do_page_map_, true);
925
926     // Specify the physical address base to test.
927     ARG_IVALUE("--paddr_base", paddr_base_);
928
929     // Specify the frequency for power spikes.
930     ARG_IVALUE("--pause_delay", pause_delay_);
931
932     // Specify the duration of each pause (for power spikes).
933     ARG_IVALUE("--pause_duration", pause_duration_);
934
935     // Disk device names
936     if (!strcmp(argv[i], "-d")) {
937       i++;
938       if (i < argc) {
939         disk_threads_++;
940         diskfilename_.push_back(string(argv[i]));
941         blocktables_.push_back(new DiskBlockTable());
942       }
943       continue;
944     }
945
946     // Set number of disk random threads for each disk write thread.
947     ARG_IVALUE("--random-threads", random_threads_);
948
949     // Set a tempfile to use in a file thread.
950     if (!strcmp(argv[i], "-f")) {
951       i++;
952       if (i < argc) {
953         file_threads_++;
954         filename_.push_back(string(argv[i]));
955       }
956       continue;
957     }
958
959     // Set a hostname to use in a network thread.
960     if (!strcmp(argv[i], "-n")) {
961       i++;
962       if (i < argc) {
963         net_threads_++;
964         ipaddrs_.push_back(string(argv[i]));
965       }
966       continue;
967     }
968
969     // Run threads that listen for incoming SAT net connections.
970     ARG_KVALUE("--listen", listen_threads_, 1);
971
972     if (CheckGoogleSpecificArgs(argc, argv, &i)) {
973       continue;
974     }
975
976     ARG_IVALUE("--channel_hash", channel_hash_);
977     ARG_IVALUE("--channel_width", channel_width_);
978
979     if (!strcmp(argv[i], "--memory_channel")) {
980       i++;
981       if (i < argc) {
982         char *channel = argv[i];
983         channels_.push_back(vector<string>());
984         while (char* next = strchr(channel, ',')) {
985           channels_.back().push_back(string(channel, next - channel));
986           channel = next + 1;
987         }
988         channels_.back().push_back(string(channel));
989       }
990       continue;
991     }
992
993     // Default:
994     PrintVersion();
995     PrintHelp();
996     if (strcmp(argv[i], "-h") && strcmp(argv[i], "--help")) {
997       printf("\n Unknown argument %s\n", argv[i]);
998       bad_status();
999       exit(1);
1000     }
1001     // Forget it, we printed the help, just bail.
1002     // We don't want to print test status, or any log parser stuff.
1003     exit(0);
1004   }
1005
1006   Logger::GlobalLogger()->SetVerbosity(verbosity_);
1007
1008   // Update relevant data members with parsed input.
1009   // Translate MB into bytes.
1010   size_ = static_cast<int64>(size_mb_) * kMegabyte;
1011
1012   // Set logfile flag.
1013   if (strcmp(logfilename_, ""))
1014     use_logfile_ = 1;
1015   // Checks valid page length.
1016   if (page_length_ &&
1017       !(page_length_ & (page_length_ - 1)) &&
1018       (page_length_ > 1023)) {
1019     // Prints if we have changed from default.
1020     if (page_length_ != kSatPageSize)
1021       logprintf(12, "Log: Updating page size to %d\n", page_length_);
1022   } else {
1023     // Revert to default page length.
1024     logprintf(6, "Process Error: "
1025               "Invalid page size %d\n", page_length_);
1026     page_length_ = kSatPageSize;
1027     return false;
1028   }
1029
1030   // Set disk_pages_ if filesize or page size changed.
1031   if (filesize != static_cast<uint64>(page_length_) *
1032                   static_cast<uint64>(disk_pages_)) {
1033     disk_pages_ = filesize / page_length_;
1034     if (disk_pages_ == 0)
1035       disk_pages_ = 1;
1036   }
1037
1038   // Validate memory channel parameters if supplied
1039   if (channels_.size()) {
1040     if (channels_.size() == 1) {
1041       channel_hash_ = 0;
1042       logprintf(7, "Log: "
1043           "Only one memory channel...deactivating interleave decoding.\n");
1044     } else if (channels_.size() > 2) {
1045       logprintf(6, "Process Error: "
1046           "Triple-channel mode not yet supported... sorry.\n");
1047       bad_status();
1048       return false;
1049     }
1050     for (uint i = 0; i < channels_.size(); i++)
1051       if (channels_[i].size() != channels_[0].size()) {
1052         logprintf(6, "Process Error: "
1053             "Channels 0 and %d have a different count of dram modules.\n", i);
1054         bad_status();
1055         return false;
1056       }
1057     if (channels_[0].size() & (channels_[0].size() - 1)) {
1058       logprintf(6, "Process Error: "
1059           "Amount of modules per memory channel is not a power of 2.\n");
1060       bad_status();
1061       return false;
1062     }
1063     if (channel_width_ < 16
1064         || channel_width_ & (channel_width_ - 1)) {
1065       logprintf(6, "Process Error: "
1066           "Channel width %d is invalid.\n", channel_width_);
1067       bad_status();
1068       return false;
1069     }
1070     if (channel_width_ / channels_[0].size() < 8) {
1071       logprintf(6, "Process Error: Chip width x%d must be x8 or greater.\n",
1072           channel_width_ / channels_[0].size());
1073       bad_status();
1074       return false;
1075     }
1076   }
1077
1078
1079   // Print each argument.
1080   for (int i = 0; i < argc; i++) {
1081     if (i)
1082       cmdline_ += " ";
1083     cmdline_ += argv[i];
1084   }
1085
1086   return true;
1087 }
1088
1089 void Sat::PrintHelp() {
1090   printf("Usage: ./sat(32|64) [options]\n"
1091          " -M mbytes        megabytes of ram to test\n"
1092          " --reserve-memory If not using hugepages, the amount of memory to "
1093          " reserve for the system\n"
1094          " -H mbytes        minimum megabytes of hugepages to require\n"
1095          " -s seconds       number of seconds to run\n"
1096          " -m threads       number of memory copy threads to run\n"
1097          " -i threads       number of memory invert threads to run\n"
1098          " -C threads       number of memory CPU stress threads to run\n"
1099          " --findfiles      find locations to do disk IO automatically\n"
1100          " -d device        add a direct write disk thread with block "
1101          "device (or file) 'device'\n"
1102          " -f filename      add a disk thread with "
1103          "tempfile 'filename'\n"
1104          " -l logfile       log output to file 'logfile'\n"
1105          " --no_timestamps  do not prefix timestamps to log messages\n"
1106          " --max_errors n   exit early after finding 'n' errors\n"
1107          " -v level         verbosity (0-20), default is 8\n"
1108          " -W               Use more CPU-stressful memory copy\n"
1109          " -A               run in degraded mode on incompatible systems\n"
1110          " -p pagesize      size in bytes of memory chunks\n"
1111          " --filesize size  size of disk IO tempfiles\n"
1112          " -n ipaddr        add a network thread connecting to "
1113          "system at 'ipaddr'\n"
1114          " --listen         run a thread to listen for and respond "
1115          "to network threads.\n"
1116          " --no_errors      run without checking for ECC or other errors\n"
1117          " --force_errors   inject false errors to test error handling\n"
1118          " --force_errors_like_crazy   inject a lot of false errors "
1119          "to test error handling\n"
1120          " -F               don't result check each transaction\n"
1121          " --stop_on_errors  Stop after finding the first error.\n"
1122          " --read-block-size     size of block for reading (-d)\n"
1123          " --write-block-size    size of block for writing (-d). If not "
1124          "defined, the size of block for writing will be defined as the "
1125          "size of block for reading\n"
1126          " --segment-size   size of segments to split disk into (-d)\n"
1127          " --cache-size     size of disk cache (-d)\n"
1128          " --blocks-per-segment  number of blocks to read/write per "
1129          "segment per iteration (-d)\n"
1130          " --read-threshold      maximum time (in us) a block read should "
1131          "take (-d)\n"
1132          " --write-threshold     maximum time (in us) a block write "
1133          "should take (-d)\n"
1134          " --random-threads      number of random threads for each disk "
1135          "write thread (-d)\n"
1136          " --destructive    write/wipe disk partition (-d)\n"
1137          " --monitor_mode   only do ECC error polling, no stress load.\n"
1138          " --cc_test        do the cache coherency testing\n"
1139          " --cc_inc_count   number of times to increment the "
1140          "cacheline's member\n"
1141          " --cc_line_count  number of cache line sized datastructures "
1142          "to allocate for the cache coherency threads to operate\n"
1143          " --cc_line_size   override the auto-detected cache line size\n"
1144          " --cpu_freq_test  enable the cpu frequency test (requires the "
1145          "--cpu_freq_threshold argument to be set)\n"
1146          " --cpu_freq_threshold  fail the cpu frequency test if the frequency "
1147          "goes below this value (specified in MHz)\n"
1148          " --cpu_freq_round round the computed frequency to this value, if set"
1149          " to zero, only round to the nearest MHz\n"
1150          " --paddr_base     allocate memory starting from this address\n"
1151          " --pause_delay    delay (in seconds) between power spikes\n"
1152          " --pause_duration duration (in seconds) of each pause\n"
1153          " --local_numa     choose memory regions associated with "
1154          "each CPU to be tested by that CPU\n"
1155          " --remote_numa    choose memory regions not associated with "
1156          "each CPU to be tested by that CPU\n"
1157          " --channel_hash   mask of address bits XORed to determine channel. "
1158          "Mask 0x40 interleaves cachelines between channels\n"
1159          " --channel_width bits     width in bits of each memory channel\n"
1160          " --memory_channel u1,u2   defines a comma-separated list of names "
1161          "for dram packages in a memory channel. Use multiple times to "
1162          "define multiple channels.\n");
1163 }
1164
1165 bool Sat::CheckGoogleSpecificArgs(int argc, char **argv, int *i) {
1166   // Do nothing, no google-specific argument on public stressapptest
1167   return false;
1168 }
1169
1170 void Sat::GoogleOsOptions(std::map<std::string, std::string> *options) {
1171   // Do nothing, no OS-specific argument on public stressapptest
1172 }
1173
1174 // Launch the SAT task threads. Returns 0 on error.
1175 void Sat::InitializeThreads() {
1176   // Memory copy threads.
1177   AcquireWorkerLock();
1178
1179   logprintf(12, "Log: Starting worker threads\n");
1180   WorkerVector *memory_vector = new WorkerVector();
1181
1182   // Error polling thread.
1183   // This may detect ECC corrected errors, disk problems, or
1184   // any other errors normally hidden from userspace.
1185   WorkerVector *error_vector = new WorkerVector();
1186   if (error_poll_) {
1187     ErrorPollThread *thread = new ErrorPollThread();
1188     thread->InitThread(total_threads_++, this, os_, patternlist_,
1189                        &continuous_status_);
1190
1191     error_vector->insert(error_vector->end(), thread);
1192   } else {
1193     logprintf(5, "Log: Skipping error poll thread due to --no_errors flag\n");
1194   }
1195   workers_map_.insert(make_pair(kErrorType, error_vector));
1196
1197   // Only start error poll threads for monitor-mode SAT,
1198   // skip all other types of worker threads.
1199   if (monitor_mode_) {
1200     ReleaseWorkerLock();
1201     return;
1202   }
1203
1204   for (int i = 0; i < memory_threads_; i++) {
1205     CopyThread *thread = new CopyThread();
1206     thread->InitThread(total_threads_++, this, os_, patternlist_,
1207                        &power_spike_status_);
1208
1209     if ((region_count_ > 1) && (region_mode_)) {
1210       int32 region = region_find(i % region_count_);
1211       cpu_set_t *cpuset = os_->FindCoreMask(region);
1212       sat_assert(cpuset);
1213       if (region_mode_ == kLocalNuma) {
1214         // Choose regions associated with this CPU.
1215         thread->set_cpu_mask(cpuset);
1216         thread->set_tag(1 << region);
1217       } else if (region_mode_ == kRemoteNuma) {
1218         // Choose regions not associated with this CPU..
1219         thread->set_cpu_mask(cpuset);
1220         thread->set_tag(region_mask_ & ~(1 << region));
1221       }
1222     } else {
1223       cpu_set_t available_cpus;
1224       thread->AvailableCpus(&available_cpus);
1225       int cores = cpuset_count(&available_cpus);
1226       // Don't restrict thread location if we have more than one
1227       // thread per core. Not so good for performance.
1228       if (cpu_stress_threads_ + memory_threads_ <= cores) {
1229         // Place a thread on alternating cores first.
1230         // This assures interleaved core use with no overlap.
1231         int nthcore = i;
1232         int nthbit = (((2 * nthcore) % cores) +
1233                       (((2 * nthcore) / cores) % 2)) % cores;
1234         cpu_set_t all_cores;
1235         cpuset_set_ab(&all_cores, 0, cores);
1236         if (!cpuset_isequal(&available_cpus, &all_cores)) {
1237           // We are assuming the bits are contiguous.
1238           // Complain if this is not so.
1239           logprintf(0, "Log: cores = %s, expected %s\n",
1240                     cpuset_format(&available_cpus).c_str(),
1241                     cpuset_format(&all_cores).c_str());
1242         }
1243
1244         // Set thread affinity.
1245         thread->set_cpu_mask_to_cpu(nthbit);
1246       }
1247     }
1248     memory_vector->insert(memory_vector->end(), thread);
1249   }
1250   workers_map_.insert(make_pair(kMemoryType, memory_vector));
1251
1252   // File IO threads.
1253   WorkerVector *fileio_vector = new WorkerVector();
1254   for (int i = 0; i < file_threads_; i++) {
1255     FileThread *thread = new FileThread();
1256     thread->InitThread(total_threads_++, this, os_, patternlist_,
1257                        &power_spike_status_);
1258     thread->SetFile(filename_[i].c_str());
1259     // Set disk threads high priority. They don't take much processor time,
1260     // but blocking them will delay disk IO.
1261     thread->SetPriority(WorkerThread::High);
1262
1263     fileio_vector->insert(fileio_vector->end(), thread);
1264   }
1265   workers_map_.insert(make_pair(kFileIOType, fileio_vector));
1266
1267   // Net IO threads.
1268   WorkerVector *netio_vector = new WorkerVector();
1269   WorkerVector *netslave_vector = new WorkerVector();
1270   if (listen_threads_ > 0) {
1271     // Create a network slave thread. This listens for connections.
1272     NetworkListenThread *thread = new NetworkListenThread();
1273     thread->InitThread(total_threads_++, this, os_, patternlist_,
1274                        &continuous_status_);
1275
1276     netslave_vector->insert(netslave_vector->end(), thread);
1277   }
1278   for (int i = 0; i < net_threads_; i++) {
1279     NetworkThread *thread = new NetworkThread();
1280     thread->InitThread(total_threads_++, this, os_, patternlist_,
1281                        &continuous_status_);
1282     thread->SetIP(ipaddrs_[i].c_str());
1283
1284     netio_vector->insert(netio_vector->end(), thread);
1285   }
1286   workers_map_.insert(make_pair(kNetIOType, netio_vector));
1287   workers_map_.insert(make_pair(kNetSlaveType, netslave_vector));
1288
1289   // Result check threads.
1290   WorkerVector *check_vector = new WorkerVector();
1291   for (int i = 0; i < check_threads_; i++) {
1292     CheckThread *thread = new CheckThread();
1293     thread->InitThread(total_threads_++, this, os_, patternlist_,
1294                        &continuous_status_);
1295
1296     check_vector->insert(check_vector->end(), thread);
1297   }
1298   workers_map_.insert(make_pair(kCheckType, check_vector));
1299
1300   // Memory invert threads.
1301   logprintf(12, "Log: Starting invert threads\n");
1302   WorkerVector *invert_vector = new WorkerVector();
1303   for (int i = 0; i < invert_threads_; i++) {
1304     InvertThread *thread = new InvertThread();
1305     thread->InitThread(total_threads_++, this, os_, patternlist_,
1306                        &continuous_status_);
1307
1308     invert_vector->insert(invert_vector->end(), thread);
1309   }
1310   workers_map_.insert(make_pair(kInvertType, invert_vector));
1311
1312   // Disk stress threads.
1313   WorkerVector *disk_vector = new WorkerVector();
1314   WorkerVector *random_vector = new WorkerVector();
1315   logprintf(12, "Log: Starting disk stress threads\n");
1316   for (int i = 0; i < disk_threads_; i++) {
1317     // Creating write threads
1318     DiskThread *thread = new DiskThread(blocktables_[i]);
1319     thread->InitThread(total_threads_++, this, os_, patternlist_,
1320                        &power_spike_status_);
1321     thread->SetDevice(diskfilename_[i].c_str());
1322     if (thread->SetParameters(read_block_size_, write_block_size_,
1323                               segment_size_, cache_size_,
1324                               blocks_per_segment_,
1325                               read_threshold_, write_threshold_,
1326                               non_destructive_)) {
1327       disk_vector->insert(disk_vector->end(), thread);
1328     } else {
1329       logprintf(12, "Log: DiskThread::SetParameters() failed\n");
1330       delete thread;
1331     }
1332
1333     for (int j = 0; j < random_threads_; j++) {
1334       // Creating random threads
1335       RandomDiskThread *rthread = new RandomDiskThread(blocktables_[i]);
1336       rthread->InitThread(total_threads_++, this, os_, patternlist_,
1337                           &power_spike_status_);
1338       rthread->SetDevice(diskfilename_[i].c_str());
1339       if (rthread->SetParameters(read_block_size_, write_block_size_,
1340                                  segment_size_, cache_size_,
1341                                  blocks_per_segment_,
1342                                  read_threshold_, write_threshold_,
1343                                  non_destructive_)) {
1344         random_vector->insert(random_vector->end(), rthread);
1345       } else {
1346       logprintf(12, "Log: RandomDiskThread::SetParameters() failed\n");
1347         delete rthread;
1348       }
1349     }
1350   }
1351
1352   workers_map_.insert(make_pair(kDiskType, disk_vector));
1353   workers_map_.insert(make_pair(kRandomDiskType, random_vector));
1354
1355   // CPU stress threads.
1356   WorkerVector *cpu_vector = new WorkerVector();
1357   logprintf(12, "Log: Starting cpu stress threads\n");
1358   for (int i = 0; i < cpu_stress_threads_; i++) {
1359     CpuStressThread *thread = new CpuStressThread();
1360     thread->InitThread(total_threads_++, this, os_, patternlist_,
1361                        &continuous_status_);
1362
1363     // Don't restrict thread location if we have more than one
1364     // thread per core. Not so good for performance.
1365     cpu_set_t available_cpus;
1366     thread->AvailableCpus(&available_cpus);
1367     int cores = cpuset_count(&available_cpus);
1368     if (cpu_stress_threads_ + memory_threads_ <= cores) {
1369       // Place a thread on alternating cores first.
1370       // Go in reverse order for CPU stress threads. This assures interleaved
1371       // core use with no overlap.
1372       int nthcore = (cores - 1) - i;
1373       int nthbit = (((2 * nthcore) % cores) +
1374                     (((2 * nthcore) / cores) % 2)) % cores;
1375       cpu_set_t all_cores;
1376       cpuset_set_ab(&all_cores, 0, cores);
1377       if (!cpuset_isequal(&available_cpus, &all_cores)) {
1378         logprintf(0, "Log: cores = %s, expected %s\n",
1379                   cpuset_format(&available_cpus).c_str(),
1380                   cpuset_format(&all_cores).c_str());
1381       }
1382
1383       // Set thread affinity.
1384       thread->set_cpu_mask_to_cpu(nthbit);
1385     }
1386
1387
1388     cpu_vector->insert(cpu_vector->end(), thread);
1389   }
1390   workers_map_.insert(make_pair(kCPUType, cpu_vector));
1391
1392   // CPU Cache Coherency Threads - one for each core available.
1393   if (cc_test_) {
1394     WorkerVector *cc_vector = new WorkerVector();
1395     logprintf(12, "Log: Starting cpu cache coherency threads\n");
1396
1397     // Allocate the shared datastructure to be worked on by the threads.
1398     cc_cacheline_data_ = reinterpret_cast<cc_cacheline_data*>(
1399         malloc(sizeof(cc_cacheline_data) * cc_cacheline_count_));
1400     sat_assert(cc_cacheline_data_ != NULL);
1401
1402     // Initialize the strucutre.
1403     memset(cc_cacheline_data_, 0,
1404            sizeof(cc_cacheline_data) * cc_cacheline_count_);
1405
1406     int num_cpus = CpuCount();
1407     char *num;
1408     // Calculate the number of cache lines needed just to give each core
1409     // its own counter.
1410     int line_size = cc_cacheline_size_;
1411     if (line_size <= 0) {
1412       line_size = CacheLineSize();
1413       if (line_size < kCacheLineSize)
1414         line_size = kCacheLineSize;
1415       logprintf(12, "Log: Using %d as cache line size\n", line_size);
1416     }
1417     // The number of cache lines needed to hold an array of num_cpus.
1418     // "num" must be the same type as cc_cacheline_data[X].num or the memory
1419     // size calculations will fail.
1420     int needed_lines = (sizeof(*num) * num_cpus + line_size - 1) / line_size;
1421     // Allocate all the nums once so that we get a single chunk
1422     // of contiguous memory.
1423 #ifdef HAVE_POSIX_MEMALIGN
1424     int err_result = posix_memalign(
1425         reinterpret_cast<void**>(&num),
1426         line_size, line_size * needed_lines * cc_cacheline_count_);
1427 #else
1428     num = reinterpret_cast<int*>(memalign(
1429         line_size, line_size * needed_lines * cc_cacheline_count_));
1430     int err_result = (num == 0);
1431 #endif
1432     sat_assert(err_result == 0);
1433
1434     int cline;
1435     for (cline = 0; cline < cc_cacheline_count_; cline++) {
1436       memset(num, 0, sizeof(*num) * num_cpus);
1437       cc_cacheline_data_[cline].num = num;
1438       num += (line_size * needed_lines) / sizeof(*num);
1439     }
1440
1441     int tnum;
1442     for (tnum = 0; tnum < num_cpus; tnum++) {
1443       CpuCacheCoherencyThread *thread =
1444           new CpuCacheCoherencyThread(cc_cacheline_data_, cc_cacheline_count_,
1445                                       tnum, num_cpus, cc_inc_count_);
1446       thread->InitThread(total_threads_++, this, os_, patternlist_,
1447                          &continuous_status_);
1448       // Pin the thread to a particular core.
1449       thread->set_cpu_mask_to_cpu(tnum);
1450
1451       // Insert the thread into the vector.
1452       cc_vector->insert(cc_vector->end(), thread);
1453     }
1454     workers_map_.insert(make_pair(kCCType, cc_vector));
1455   }
1456
1457   if (cpu_freq_test_) {
1458     // Create the frequency test thread.
1459     logprintf(5, "Log: Running cpu frequency test: threshold set to %dMHz.\n",
1460               cpu_freq_threshold_);
1461     CpuFreqThread *thread = new CpuFreqThread(CpuCount(), cpu_freq_threshold_,
1462                                               cpu_freq_round_);
1463     // This thread should be paused when other threads are paused.
1464     thread->InitThread(total_threads_++, this, os_, NULL,
1465                        &power_spike_status_);
1466
1467     WorkerVector *cpu_freq_vector = new WorkerVector();
1468     cpu_freq_vector->insert(cpu_freq_vector->end(), thread);
1469     workers_map_.insert(make_pair(kCPUFreqType, cpu_freq_vector));
1470   }
1471
1472   ReleaseWorkerLock();
1473 }
1474
1475 // Return the number of cpus actually present in the machine.
1476 int Sat::CpuCount() {
1477   return sysconf(_SC_NPROCESSORS_CONF);
1478 }
1479
1480 // Return the worst case (largest) cache line size of the various levels of
1481 // cache actually prsent in the machine.
1482 int Sat::CacheLineSize() {
1483   int max_linesize = sysconf(_SC_LEVEL1_DCACHE_LINESIZE);
1484   int linesize = sysconf(_SC_LEVEL2_CACHE_LINESIZE);
1485   if (linesize > max_linesize) max_linesize = linesize;
1486   linesize = sysconf(_SC_LEVEL3_CACHE_LINESIZE);
1487   if (linesize > max_linesize) max_linesize = linesize;
1488   linesize = sysconf(_SC_LEVEL4_CACHE_LINESIZE);
1489   if (linesize > max_linesize) max_linesize = linesize;
1490   return max_linesize;
1491 }
1492
1493 // Notify and reap worker threads.
1494 void Sat::JoinThreads() {
1495   logprintf(12, "Log: Joining worker threads\n");
1496   power_spike_status_.StopWorkers();
1497   continuous_status_.StopWorkers();
1498
1499   AcquireWorkerLock();
1500   for (WorkerMap::const_iterator map_it = workers_map_.begin();
1501        map_it != workers_map_.end(); ++map_it) {
1502     for (WorkerVector::const_iterator it = map_it->second->begin();
1503          it != map_it->second->end(); ++it) {
1504       logprintf(12, "Log: Joining thread %d\n", (*it)->ThreadID());
1505       (*it)->JoinThread();
1506     }
1507   }
1508   ReleaseWorkerLock();
1509
1510   QueueStats();
1511
1512   // Finish up result checking.
1513   // Spawn 4 check threads to minimize check time.
1514   logprintf(12, "Log: Finished countdown, begin to result check\n");
1515   WorkerStatus reap_check_status;
1516   WorkerVector reap_check_vector;
1517
1518   // No need for check threads for monitor mode.
1519   if (!monitor_mode_) {
1520     // Initialize the check threads.
1521     for (int i = 0; i < fill_threads_; i++) {
1522       CheckThread *thread = new CheckThread();
1523       thread->InitThread(total_threads_++, this, os_, patternlist_,
1524                          &reap_check_status);
1525       logprintf(12, "Log: Finished countdown, begin to result check\n");
1526       reap_check_vector.push_back(thread);
1527     }
1528   }
1529
1530   reap_check_status.Initialize();
1531   // Check threads should be marked to stop ASAP.
1532   reap_check_status.StopWorkers();
1533
1534   // Spawn the check threads.
1535   for (WorkerVector::const_iterator it = reap_check_vector.begin();
1536        it != reap_check_vector.end(); ++it) {
1537     logprintf(12, "Log: Spawning thread %d\n", (*it)->ThreadID());
1538     (*it)->SpawnThread();
1539   }
1540
1541   // Join the check threads.
1542   for (WorkerVector::const_iterator it = reap_check_vector.begin();
1543        it != reap_check_vector.end(); ++it) {
1544     logprintf(12, "Log: Joining thread %d\n", (*it)->ThreadID());
1545     (*it)->JoinThread();
1546   }
1547
1548   // Reap all children. Stopped threads should have already ended.
1549   // Result checking threads will end when they have finished
1550   // result checking.
1551   logprintf(12, "Log: Join all outstanding threads\n");
1552
1553   // Find all errors.
1554   errorcount_ = GetTotalErrorCount();
1555
1556   AcquireWorkerLock();
1557   for (WorkerMap::const_iterator map_it = workers_map_.begin();
1558        map_it != workers_map_.end(); ++map_it) {
1559     for (WorkerVector::const_iterator it = map_it->second->begin();
1560          it != map_it->second->end(); ++it) {
1561       logprintf(12, "Log: Reaping thread status %d\n", (*it)->ThreadID());
1562       if ((*it)->GetStatus() != 1) {
1563         logprintf(0, "Process Error: Thread %d failed with status %d at "
1564                   "%.2f seconds\n",
1565                   (*it)->ThreadID(), (*it)->GetStatus(),
1566                   (*it)->GetRunDurationUSec()*1.0/1000000);
1567         bad_status();
1568       }
1569       int priority = 12;
1570       if ((*it)->GetErrorCount())
1571         priority = 5;
1572       logprintf(priority, "Log: Thread %d found %lld hardware incidents\n",
1573                 (*it)->ThreadID(), (*it)->GetErrorCount());
1574     }
1575   }
1576   ReleaseWorkerLock();
1577
1578
1579   // Add in any errors from check threads.
1580   for (WorkerVector::const_iterator it = reap_check_vector.begin();
1581        it != reap_check_vector.end(); ++it) {
1582     logprintf(12, "Log: Reaping thread status %d\n", (*it)->ThreadID());
1583     if ((*it)->GetStatus() != 1) {
1584       logprintf(0, "Process Error: Thread %d failed with status %d at "
1585                 "%.2f seconds\n",
1586                 (*it)->ThreadID(), (*it)->GetStatus(),
1587                 (*it)->GetRunDurationUSec()*1.0/1000000);
1588       bad_status();
1589     }
1590     errorcount_ += (*it)->GetErrorCount();
1591     int priority = 12;
1592     if ((*it)->GetErrorCount())
1593       priority = 5;
1594     logprintf(priority, "Log: Thread %d found %lld hardware incidents\n",
1595               (*it)->ThreadID(), (*it)->GetErrorCount());
1596     delete (*it);
1597   }
1598   reap_check_vector.clear();
1599   reap_check_status.Destroy();
1600 }
1601
1602 // Print queuing information.
1603 void Sat::QueueStats() {
1604   finelock_q_->QueueAnalysis();
1605 }
1606
1607 void Sat::AnalysisAllStats() {
1608   float max_runtime_sec = 0.;
1609   float total_data = 0.;
1610   float total_bandwidth = 0.;
1611   float thread_runtime_sec = 0.;
1612
1613   for (WorkerMap::const_iterator map_it = workers_map_.begin();
1614        map_it != workers_map_.end(); ++map_it) {
1615     for (WorkerVector::const_iterator it = map_it->second->begin();
1616          it != map_it->second->end(); ++it) {
1617       thread_runtime_sec = (*it)->GetRunDurationUSec()*1.0/1000000;
1618       total_data += (*it)->GetMemoryCopiedData();
1619       total_data += (*it)->GetDeviceCopiedData();
1620       if (thread_runtime_sec > max_runtime_sec) {
1621         max_runtime_sec = thread_runtime_sec;
1622       }
1623     }
1624   }
1625
1626   total_bandwidth = total_data / max_runtime_sec;
1627
1628   logprintf(0, "Stats: Completed: %.2fM in %.2fs %.2fMB/s, "
1629             "with %d hardware incidents, %d errors\n",
1630             total_data,
1631             max_runtime_sec,
1632             total_bandwidth,
1633             errorcount_,
1634             statuscount_);
1635 }
1636
1637 void Sat::MemoryStats() {
1638   float memcopy_data = 0.;
1639   float memcopy_bandwidth = 0.;
1640   WorkerMap::const_iterator mem_it = workers_map_.find(
1641       static_cast<int>(kMemoryType));
1642   WorkerMap::const_iterator file_it = workers_map_.find(
1643       static_cast<int>(kFileIOType));
1644   sat_assert(mem_it != workers_map_.end());
1645   sat_assert(file_it != workers_map_.end());
1646   for (WorkerVector::const_iterator it = mem_it->second->begin();
1647        it != mem_it->second->end(); ++it) {
1648     memcopy_data += (*it)->GetMemoryCopiedData();
1649     memcopy_bandwidth += (*it)->GetMemoryBandwidth();
1650   }
1651   for (WorkerVector::const_iterator it = file_it->second->begin();
1652        it != file_it->second->end(); ++it) {
1653     memcopy_data += (*it)->GetMemoryCopiedData();
1654     memcopy_bandwidth += (*it)->GetMemoryBandwidth();
1655   }
1656   GoogleMemoryStats(&memcopy_data, &memcopy_bandwidth);
1657   logprintf(4, "Stats: Memory Copy: %.2fM at %.2fMB/s\n",
1658             memcopy_data,
1659             memcopy_bandwidth);
1660 }
1661
1662 void Sat::GoogleMemoryStats(float *memcopy_data,
1663                             float *memcopy_bandwidth) {
1664   // Do nothing, should be implemented by subclasses.
1665 }
1666
1667 void Sat::FileStats() {
1668   float file_data = 0.;
1669   float file_bandwidth = 0.;
1670   WorkerMap::const_iterator file_it = workers_map_.find(
1671       static_cast<int>(kFileIOType));
1672   sat_assert(file_it != workers_map_.end());
1673   for (WorkerVector::const_iterator it = file_it->second->begin();
1674        it != file_it->second->end(); ++it) {
1675     file_data += (*it)->GetDeviceCopiedData();
1676     file_bandwidth += (*it)->GetDeviceBandwidth();
1677   }
1678   logprintf(4, "Stats: File Copy: %.2fM at %.2fMB/s\n",
1679             file_data,
1680             file_bandwidth);
1681 }
1682
1683 void Sat::CheckStats() {
1684   float check_data = 0.;
1685   float check_bandwidth = 0.;
1686   WorkerMap::const_iterator check_it = workers_map_.find(
1687       static_cast<int>(kCheckType));
1688   sat_assert(check_it != workers_map_.end());
1689   for (WorkerVector::const_iterator it = check_it->second->begin();
1690        it != check_it->second->end(); ++it) {
1691     check_data += (*it)->GetMemoryCopiedData();
1692     check_bandwidth += (*it)->GetMemoryBandwidth();
1693   }
1694   logprintf(4, "Stats: Data Check: %.2fM at %.2fMB/s\n",
1695             check_data,
1696             check_bandwidth);
1697 }
1698
1699 void Sat::NetStats() {
1700   float net_data = 0.;
1701   float net_bandwidth = 0.;
1702   WorkerMap::const_iterator netio_it = workers_map_.find(
1703       static_cast<int>(kNetIOType));
1704   WorkerMap::const_iterator netslave_it = workers_map_.find(
1705       static_cast<int>(kNetSlaveType));
1706   sat_assert(netio_it != workers_map_.end());
1707   sat_assert(netslave_it != workers_map_.end());
1708   for (WorkerVector::const_iterator it = netio_it->second->begin();
1709        it != netio_it->second->end(); ++it) {
1710     net_data += (*it)->GetDeviceCopiedData();
1711     net_bandwidth += (*it)->GetDeviceBandwidth();
1712   }
1713   for (WorkerVector::const_iterator it = netslave_it->second->begin();
1714        it != netslave_it->second->end(); ++it) {
1715     net_data += (*it)->GetDeviceCopiedData();
1716     net_bandwidth += (*it)->GetDeviceBandwidth();
1717   }
1718   logprintf(4, "Stats: Net Copy: %.2fM at %.2fMB/s\n",
1719             net_data,
1720             net_bandwidth);
1721 }
1722
1723 void Sat::InvertStats() {
1724   float invert_data = 0.;
1725   float invert_bandwidth = 0.;
1726   WorkerMap::const_iterator invert_it = workers_map_.find(
1727       static_cast<int>(kInvertType));
1728   sat_assert(invert_it != workers_map_.end());
1729   for (WorkerVector::const_iterator it = invert_it->second->begin();
1730        it != invert_it->second->end(); ++it) {
1731     invert_data += (*it)->GetMemoryCopiedData();
1732     invert_bandwidth += (*it)->GetMemoryBandwidth();
1733   }
1734   logprintf(4, "Stats: Invert Data: %.2fM at %.2fMB/s\n",
1735             invert_data,
1736             invert_bandwidth);
1737 }
1738
1739 void Sat::DiskStats() {
1740   float disk_data = 0.;
1741   float disk_bandwidth = 0.;
1742   WorkerMap::const_iterator disk_it = workers_map_.find(
1743       static_cast<int>(kDiskType));
1744   WorkerMap::const_iterator random_it = workers_map_.find(
1745       static_cast<int>(kRandomDiskType));
1746   sat_assert(disk_it != workers_map_.end());
1747   sat_assert(random_it != workers_map_.end());
1748   for (WorkerVector::const_iterator it = disk_it->second->begin();
1749        it != disk_it->second->end(); ++it) {
1750     disk_data += (*it)->GetDeviceCopiedData();
1751     disk_bandwidth += (*it)->GetDeviceBandwidth();
1752   }
1753   for (WorkerVector::const_iterator it = random_it->second->begin();
1754        it != random_it->second->end(); ++it) {
1755     disk_data += (*it)->GetDeviceCopiedData();
1756     disk_bandwidth += (*it)->GetDeviceBandwidth();
1757   }
1758
1759   logprintf(4, "Stats: Disk: %.2fM at %.2fMB/s\n",
1760             disk_data,
1761             disk_bandwidth);
1762 }
1763
1764 // Process worker thread data for bandwidth information, and error results.
1765 // You can add more methods here just subclassing SAT.
1766 void Sat::RunAnalysis() {
1767   AnalysisAllStats();
1768   MemoryStats();
1769   FileStats();
1770   NetStats();
1771   CheckStats();
1772   InvertStats();
1773   DiskStats();
1774 }
1775
1776 // Get total error count, summing across all threads..
1777 int64 Sat::GetTotalErrorCount() {
1778   int64 errors = 0;
1779
1780   AcquireWorkerLock();
1781   for (WorkerMap::const_iterator map_it = workers_map_.begin();
1782        map_it != workers_map_.end(); ++map_it) {
1783     for (WorkerVector::const_iterator it = map_it->second->begin();
1784          it != map_it->second->end(); ++it) {
1785       errors += (*it)->GetErrorCount();
1786     }
1787   }
1788   ReleaseWorkerLock();
1789   return errors;
1790 }
1791
1792
1793 void Sat::SpawnThreads() {
1794   logprintf(12, "Log: Initializing WorkerStatus objects\n");
1795   power_spike_status_.Initialize();
1796   continuous_status_.Initialize();
1797   logprintf(12, "Log: Spawning worker threads\n");
1798   for (WorkerMap::const_iterator map_it = workers_map_.begin();
1799        map_it != workers_map_.end(); ++map_it) {
1800     for (WorkerVector::const_iterator it = map_it->second->begin();
1801          it != map_it->second->end(); ++it) {
1802       logprintf(12, "Log: Spawning thread %d\n", (*it)->ThreadID());
1803       (*it)->SpawnThread();
1804     }
1805   }
1806 }
1807
1808 // Delete used worker thread objects.
1809 void Sat::DeleteThreads() {
1810   logprintf(12, "Log: Deleting worker threads\n");
1811   for (WorkerMap::const_iterator map_it = workers_map_.begin();
1812        map_it != workers_map_.end(); ++map_it) {
1813     for (WorkerVector::const_iterator it = map_it->second->begin();
1814          it != map_it->second->end(); ++it) {
1815       logprintf(12, "Log: Deleting thread %d\n", (*it)->ThreadID());
1816       delete (*it);
1817     }
1818     delete map_it->second;
1819   }
1820   workers_map_.clear();
1821   logprintf(12, "Log: Destroying WorkerStatus objects\n");
1822   power_spike_status_.Destroy();
1823   continuous_status_.Destroy();
1824 }
1825
1826 namespace {
1827 // Calculates the next time an action in Sat::Run() should occur, based on a
1828 // schedule derived from a start point and a regular frequency.
1829 //
1830 // Using frequencies instead of intervals with their accompanying drift allows
1831 // users to better predict when the actions will occur throughout a run.
1832 //
1833 // Arguments:
1834 //   frequency: seconds
1835 //   start: unixtime
1836 //   now: unixtime
1837 //
1838 // Returns: unixtime
1839 inline time_t NextOccurance(time_t frequency, time_t start, time_t now) {
1840   return start + frequency + (((now - start) / frequency) * frequency);
1841 }
1842 }
1843
1844 // Run the actual test.
1845 bool Sat::Run() {
1846   // Install signal handlers to gracefully exit in the middle of a run.
1847   //
1848   // Why go through this whole rigmarole?  It's the only standards-compliant
1849   // (C++ and POSIX) way to handle signals in a multithreaded program.
1850   // Specifically:
1851   //
1852   // 1) (C++) The value of a variable not of type "volatile sig_atomic_t" is
1853   //    unspecified upon entering a signal handler and, if modified by the
1854   //    handler, is unspecified after leaving the handler.
1855   //
1856   // 2) (POSIX) After the value of a variable is changed in one thread, another
1857   //    thread is only guaranteed to see the new value after both threads have
1858   //    acquired or released the same mutex or rwlock, synchronized to the
1859   //    same barrier, or similar.
1860   //
1861   // #1 prevents the use of #2 in a signal handler, so the signal handler must
1862   // be called in the same thread that reads the "volatile sig_atomic_t"
1863   // variable it sets.  We enforce that by blocking the signals in question in
1864   // the worker threads, forcing them to be handled by this thread.
1865   logprintf(12, "Log: Installing signal handlers\n");
1866   sigset_t new_blocked_signals;
1867   sigemptyset(&new_blocked_signals);
1868   sigaddset(&new_blocked_signals, SIGINT);
1869   sigaddset(&new_blocked_signals, SIGTERM);
1870   sigset_t prev_blocked_signals;
1871   pthread_sigmask(SIG_BLOCK, &new_blocked_signals, &prev_blocked_signals);
1872   sighandler_t prev_sigint_handler = signal(SIGINT, SatHandleBreak);
1873   sighandler_t prev_sigterm_handler = signal(SIGTERM, SatHandleBreak);
1874
1875   // Kick off all the worker threads.
1876   logprintf(12, "Log: Launching worker threads\n");
1877   InitializeThreads();
1878   SpawnThreads();
1879   pthread_sigmask(SIG_SETMASK, &prev_blocked_signals, NULL);
1880
1881   logprintf(12, "Log: Starting countdown with %d seconds\n", runtime_seconds_);
1882
1883   // In seconds.
1884   static const time_t kSleepFrequency = 5;
1885   // All of these are in seconds.  You probably want them to be >=
1886   // kSleepFrequency and multiples of kSleepFrequency, but neither is necessary.
1887   static const time_t kInjectionFrequency = 10;
1888   static const time_t kPrintFrequency = 10;
1889
1890   const time_t start = time(NULL);
1891   const time_t end = start + runtime_seconds_;
1892   time_t now = start;
1893   time_t next_print = start + kPrintFrequency;
1894   time_t next_pause = start + pause_delay_;
1895   time_t next_resume = 0;
1896   time_t next_injection;
1897   if (crazy_error_injection_) {
1898     next_injection = start + kInjectionFrequency;
1899   } else {
1900     next_injection = 0;
1901   }
1902
1903   while (now < end) {
1904     // This is an int because it's for logprintf().
1905     const int seconds_remaining = end - now;
1906
1907     if (user_break_) {
1908       // Handle early exit.
1909       logprintf(0, "Log: User exiting early (%d seconds remaining)\n",
1910                 seconds_remaining);
1911       break;
1912     }
1913
1914     // If we have an error limit, check it here and see if we should exit.
1915     if (max_errorcount_ != 0) {
1916       uint64 errors = GetTotalErrorCount();
1917       if (errors > max_errorcount_) {
1918         logprintf(0, "Log: Exiting early (%d seconds remaining) "
1919                      "due to excessive failures (%lld)\n",
1920                   seconds_remaining,
1921                   errors);
1922         break;
1923       }
1924     }
1925
1926     if (now >= next_print) {
1927       // Print a count down message.
1928       logprintf(5, "Log: Seconds remaining: %d\n", seconds_remaining);
1929       next_print = NextOccurance(kPrintFrequency, start, now);
1930     }
1931
1932     if (next_injection && now >= next_injection) {
1933       // Inject an error.
1934       logprintf(4, "Log: Injecting error (%d seconds remaining)\n",
1935                 seconds_remaining);
1936       struct page_entry src;
1937       GetValid(&src);
1938       src.pattern = patternlist_->GetPattern(0);
1939       PutValid(&src);
1940       next_injection = NextOccurance(kInjectionFrequency, start, now);
1941     }
1942
1943     if (next_pause && now >= next_pause) {
1944       // Tell worker threads to pause in preparation for a power spike.
1945       logprintf(4, "Log: Pausing worker threads in preparation for power spike "
1946                 "(%d seconds remaining)\n", seconds_remaining);
1947       power_spike_status_.PauseWorkers();
1948       logprintf(12, "Log: Worker threads paused\n");
1949       next_pause = 0;
1950       next_resume = now + pause_duration_;
1951     }
1952
1953     if (next_resume && now >= next_resume) {
1954       // Tell worker threads to resume in order to cause a power spike.
1955       logprintf(4, "Log: Resuming worker threads to cause a power spike (%d "
1956                 "seconds remaining)\n", seconds_remaining);
1957       power_spike_status_.ResumeWorkers();
1958       logprintf(12, "Log: Worker threads resumed\n");
1959       next_pause = NextOccurance(pause_delay_, start, now);
1960       next_resume = 0;
1961     }
1962
1963     sat_sleep(NextOccurance(kSleepFrequency, start, now) - now);
1964     now = time(NULL);
1965   }
1966
1967   JoinThreads();
1968
1969   logprintf(0, "Stats: Found %lld hardware incidents\n", errorcount_);
1970
1971   if (!monitor_mode_)
1972     RunAnalysis();
1973
1974   DeleteThreads();
1975
1976   logprintf(12, "Log: Uninstalling signal handlers\n");
1977   signal(SIGINT, prev_sigint_handler);
1978   signal(SIGTERM, prev_sigterm_handler);
1979
1980   return true;
1981 }
1982
1983 // Clean up all resources.
1984 bool Sat::Cleanup() {
1985   g_sat = NULL;
1986   Logger::GlobalLogger()->StopThread();
1987   Logger::GlobalLogger()->SetStdoutOnly();
1988   if (logfile_) {
1989     close(logfile_);
1990     logfile_ = 0;
1991   }
1992   if (patternlist_) {
1993     patternlist_->Destroy();
1994     delete patternlist_;
1995     patternlist_ = 0;
1996   }
1997   if (os_) {
1998     os_->FreeTestMem();
1999     delete os_;
2000     os_ = 0;
2001   }
2002   if (empty_) {
2003     delete empty_;
2004     empty_ = 0;
2005   }
2006   if (valid_) {
2007     delete valid_;
2008     valid_ = 0;
2009   }
2010   if (finelock_q_) {
2011     delete finelock_q_;
2012     finelock_q_ = 0;
2013   }
2014   if (page_bitmap_) {
2015     delete[] page_bitmap_;
2016   }
2017
2018   for (size_t i = 0; i < blocktables_.size(); i++) {
2019     delete blocktables_[i];
2020   }
2021
2022   if (cc_cacheline_data_) {
2023     // The num integer arrays for all the cacheline structures are
2024     // allocated as a single chunk. The pointers in the cacheline struct
2025     // are populated accordingly. Hence calling free on the first
2026     // cacheline's num's address is going to free the entire array.
2027     // TODO(aganti): Refactor this to have a class for the cacheline
2028     // structure (currently defined in worker.h) and clean this up
2029     // in the destructor of that class.
2030     if (cc_cacheline_data_[0].num) {
2031       free(cc_cacheline_data_[0].num);
2032     }
2033     free(cc_cacheline_data_);
2034   }
2035
2036   sat_assert(0 == pthread_mutex_destroy(&worker_lock_));
2037
2038   return true;
2039 }
2040
2041
2042 // Pretty print really obvious results.
2043 bool Sat::PrintResults() {
2044   bool result = true;
2045
2046   logprintf(4, "\n");
2047   if (statuscount_) {
2048     logprintf(4, "Status: FAIL - test encountered procedural errors\n");
2049     result = false;
2050   } else if (errorcount_) {
2051     logprintf(4, "Status: FAIL - test discovered HW problems\n");
2052     result = false;
2053   } else {
2054     logprintf(4, "Status: PASS - please verify no corrected errors\n");
2055   }
2056   logprintf(4, "\n");
2057
2058   return result;
2059 }
2060
2061 // Helper functions.
2062 void Sat::AcquireWorkerLock() {
2063   sat_assert(0 == pthread_mutex_lock(&worker_lock_));
2064 }
2065 void Sat::ReleaseWorkerLock() {
2066   sat_assert(0 == pthread_mutex_unlock(&worker_lock_));
2067 }
2068
2069 void logprintf(int priority, const char *format, ...) {
2070   va_list args;
2071   va_start(args, format);
2072   Logger::GlobalLogger()->VLogF(priority, format, args);
2073   va_end(args);
2074 }
2075
2076 // Stop the logging thread and verify any pending data is written to the log.
2077 void logstop() {
2078   Logger::GlobalLogger()->StopThread();
2079 }
2080