1 // Copyright 2006 Google Inc. All Rights Reserved.
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 // sat.cc : a stress test for stressful testing
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.
21 // stressapptest can be run using memory only, or using many system components.
33 #include <sys/times.h>
36 // #define __USE_LARGEFILE64
42 // This file must work with autoconf on its public version,
43 // so these includes are correct.
44 #include "disk_blocks.h"
51 // stressapptest versioning here.
52 #ifndef PACKAGE_VERSION
53 static const char* kVersion = "1.0.0";
55 static const char* kVersion = PACKAGE_VERSION;
58 // Global stressapptest reference, for use by signal handler.
59 // This makes Sat objects not safe for multiple instances.
63 // Signal handler for catching break or kill.
65 // This must be installed after g_sat is assigned and while there is a single
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) {
75 // Opens the logfile for writing if necessary
76 bool Sat::InitializeLogfile() {
79 logfile_ = open(logfilename_,
84 #elif defined(O_FSYNC)
88 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
90 printf("Fatal Error: cannot open file %s for logging\n",
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",
103 Logger::GlobalLogger()->SetLogFd(logfile_);
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.
114 if (run_on_anything_) {
115 logprintf(1, "Log: Running DEBUG version of SAT, "
116 "with significantly reduced coverage.\n");
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");
124 #elif !defined CHECKOPTS
125 #error Build system regression - COPTS disregarded.
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 "
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");
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");
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_);
154 // Use all memory if no size is specified.
156 size_mb_ = os_->FindFreeMemSize() / kMegabyte;
157 size_ = static_cast<int64>(size_mb_) * kMegabyte;
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();
165 while (!locations.empty()) {
166 // Copy and remove the disk name.
167 string disk = locations.back();
168 locations.pop_back();
170 logprintf(12, "Log: disk at %s\n", disk.c_str());
172 filename_.push_back(disk + "/sat_disk.a");
174 filename_.push_back(disk + "/sat_disk.b");
178 // We'd better have some memory by this point.
180 logprintf(0, "Process Error: No memory found to test.\n");
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");
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",
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.
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");
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_);
224 logprintf(0, "Process Error: failed to allocate memory\n");
231 // Sets up access to data patterns
232 bool Sat::InitializePatterns() {
233 // Initialize pattern data.
234 patternlist_ = new PatternList();
236 logprintf(0, "Process Error: failed to allocate patterns\n");
240 if (!patternlist_->Initialize()) {
241 logprintf(0, "Process Error: failed to initialize patternlist\n");
248 // Get any valid page, no tag specified.
249 bool Sat::GetValid(struct page_entry *pe) {
250 return GetValid(pe, kDontCareTag);
254 // Fetch and return empty and full pages into the empty and full pools.
255 bool Sat::GetValid(struct page_entry *pe, int32 tag) {
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);
264 pe->addr = os_->PrepareTestMem(pe->offset, page_length_); // Map it.
266 // Tag this access and current pattern.
267 pe->ts = os_->GetTimestamp();
268 pe->lastpattern = pe->pattern;
270 return (pe->addr != 0); // Return success or failure.
275 bool Sat::PutValid(struct page_entry *pe) {
277 os_->ReleaseTestMem(pe->addr, pe->offset, page_length_); // Unmap the page.
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);
289 // Get an empty page with any tag.
290 bool Sat::GetEmpty(struct page_entry *pe) {
291 return GetEmpty(pe, kDontCareTag);
294 bool Sat::GetEmpty(struct page_entry *pe, int32 tag) {
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);
303 pe->addr = os_->PrepareTestMem(pe->offset, page_length_); // Map it.
304 return (pe->addr != 0); // Return success or failure.
309 bool Sat::PutEmpty(struct page_entry *pe) {
311 os_->ReleaseTestMem(pe->addr, pe->offset, page_length_); // Unmap the page.
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);
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() {
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);
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];
341 // Mark every page as 0, not seen.
342 memset(bitmap, 0, arraysize);
344 page_bitmap_size_ = maxsize;
345 page_bitmap_ = bitmap;
348 // Add the 4k pages in this block to the array of pages SAT has seen.
349 void Sat::AddrMapUpdate(struct page_entry *pe) {
353 // Go through 4k page blocks.
354 uint64 arraysize = page_bitmap_size_ / 4096 / 8;
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);
360 uint32 offset = paddr / 4096 / 8;
361 unsigned char mask = 1 << ((paddr / 4096) % 8);
363 if (offset >= arraysize) {
364 logprintf(0, "Process Error: Physical address %#llx is "
365 "greater than expected %#llx.\n",
366 paddr, page_bitmap_size_);
369 page_bitmap_[offset] |= mask;
373 // Print out the physical memory ranges that SAT has accessed.
374 void Sat::AddrMapPrint() {
378 uint64 pages = page_bitmap_size_ / 4096;
380 uint64 last_page = 0;
381 bool valid_range = false;
383 logprintf(4, "Log: Printing tested physical ranges.\n");
385 for (uint64 i = 0; i < pages; i ++) {
387 unsigned char mask = 1 << (i % 8);
389 bool touched = page_bitmap_[offset] & mask;
390 if (touched && !valid_range) {
392 last_page = i * 4096;
393 } else if (!touched && valid_range) {
395 logprintf(4, "Log: %#016llx - %#016llx\n", last_page, (i * 4096) - 1);
398 logprintf(4, "Log: Done printing physical ranges.\n");
401 // Initializes page lists and fills pages with data patterns.
402 bool Sat::InitializePages() {
404 // Calculate needed page totals.
405 int64 neededpages = memory_threads_ +
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.
418 freepages_ = (pages_ / 100) + (2 * neededpages);
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_));
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));
439 logprintf(12, "Log: Allocating pages, Total: %lld Free: %lld\n",
443 // Initialize page locations.
444 for (int64 i = 0; i < pages_; i++) {
445 struct page_entry pe;
447 pe.offset = i * page_length_;
448 result &= PutEmpty(&pe);
452 logprintf(0, "Process Error: while initializing empty_ list\n");
457 // Fill valid pages with test patterns.
458 // Use fill threads to do this.
459 WorkerStatus fill_status;
460 WorkerVector fill_vector;
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.
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);
478 fill_vector.push_back(thread);
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();
487 // Reap the finished fill threads.
488 for (WorkerVector::const_iterator it = fill_vector.begin();
489 it != fill_vector.end(); ++it) {
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);
501 fill_status.Destroy();
502 logprintf(12, "Log: Done filling pages.\n");
503 logprintf(12, "Log: Allocating pages.\n");
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);
516 pe.tag = 1 << region;
517 region_mask_ |= pe.tag;
519 // Generate a physical region map
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);
529 result &= PutValid(&pe);
532 logprintf(0, "Log: didn't tag all pages. %d - %d = %d\n",
533 pages_, i, pages_ - i);
537 logprintf(12, "Log: Done allocating pages.\n");
541 for (int i = 0; i < 32; i++) {
542 if (region_mask_ & (1 << i)) {
544 logprintf(12, "Log: Region %d: %d.\n", i, region_[i]);
547 logprintf(5, "Log: Region mask: 0x%x\n", region_mask_);
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());
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() {
568 // Initializes sync'd log file to ensure output is saved.
569 if (!InitializeLogfile())
571 Logger::GlobalLogger()->SetTimestampLogging(log_timestamps_);
572 Logger::GlobalLogger()->StartThread();
574 logprintf(5, "Log: Commandline - %s\n", cmdline_.c_str());
577 std::map<std::string, std::string> options;
579 GoogleOsOptions(&options);
581 // Initialize OS/Hardware interface.
582 os_ = OsLayerFactory(options);
588 if (min_hugepages_mbytes_ > 0)
589 os_->SetMinimumHugepagesSize(min_hugepages_mbytes_ * kMegabyte);
592 os_->SetReserveSize(reserve_mb_);
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_);
602 if (!os_->Initialize()) {
603 logprintf(0, "Process Error: Failed to initialize OS layer\n");
609 // Checks that OS/Build/Platform is supported.
610 if (!CheckEnvironment())
613 if (error_injection_)
614 os_->set_error_injection(true);
616 // Run SAT in monitor only mode, do not continue to allocate resources.
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");
624 // Allocate the memory to test.
625 if (!AllocateMemory())
628 logprintf(5, "Stats: Starting SAT, %dM, %d seconds\n",
629 static_cast<int>(size_/kMegabyte),
632 if (!InitializePatterns())
635 // Initialize memory allocation.
636 pages_ = size_ / page_length_;
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)
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))
652 if (!InitializePages()) {
653 logprintf(0, "Process Error: Initialize Pages failed\n");
660 // Constructor and destructor.
662 // Set defaults, command line might override these.
663 runtime_seconds_ = 20;
664 page_length_ = kSatPageSize;
665 disk_pages_ = kSatDiskPage;
668 size_ = size_mb_ * kMegabyte;
670 min_hugepages_mbytes_ = 0;
673 channel_hash_ = kCacheLineSize;
678 Logger::GlobalLogger()->SetVerbosity(verbosity_);
681 run_on_anything_ = 0;
684 log_timestamps_ = true;
685 // Detect 32/64 bit binary.
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;
695 do_page_map_ = false;
697 page_bitmap_size_ = 0;
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.
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.
711 sat_assert(0 == pthread_mutex_init(&worker_lock_, NULL));
715 // Default to autodetect number of cpus, and run that many threads.
716 memory_threads_ = -1;
720 cpu_stress_threads_ = 0;
726 for (int i = 0; i < 32; i++) {
737 // Default to use fine-grain lock for better performance.
738 pe_q_implementation_ = SAT_FINELOCK;
744 read_block_size_ = 512;
745 write_block_size_ = -1;
748 blocks_per_segment_ = -1;
749 read_threshold_ = -1;
750 write_threshold_ = -1;
751 non_destructive_ = 1;
757 pause_duration_ = 15;
762 // We need to have called Cleanup() at this point.
763 // We should probably enforce this.
767 #define ARG_KVALUE(argument, variable, value) \
768 if (!strcmp(argv[i], argument)) { \
773 #define ARG_IVALUE(argument, variable) \
774 if (!strcmp(argv[i], argument)) { \
777 variable = strtoull(argv[i], NULL, 0); \
781 #define ARG_SVALUE(argument, variable) \
782 if (!strcmp(argv[i], argument)) { \
785 snprintf(variable, sizeof(variable), "%s", argv[i]); \
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) {
794 uint64 filesize = page_length_ * disk_pages_;
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);
801 // Set number of megabyte to use.
802 ARG_IVALUE("-M", size_mb_);
804 // Specify the amount of megabytes to be reserved for system.
805 ARG_IVALUE("--reserve_memory", reserve_mb_);
807 // Set minimum megabytes of hugepages to require.
808 ARG_IVALUE("-H", min_hugepages_mbytes_);
810 // Set number of seconds to run.
811 ARG_IVALUE("-s", runtime_seconds_);
813 // Set number of memory copy threads.
814 ARG_IVALUE("-m", memory_threads_);
816 // Set number of memory invert threads.
817 ARG_IVALUE("-i", invert_threads_);
819 // Set number of check-only threads.
820 ARG_IVALUE("-c", check_threads_);
822 // Set number of cache line size datastructures.
823 ARG_IVALUE("--cc_inc_count", cc_inc_count_);
825 // Set number of cache line size datastructures
826 ARG_IVALUE("--cc_line_count", cc_cacheline_count_);
828 // Override the detected or assumed cache line size.
829 ARG_IVALUE("--cc_line_size", cc_cacheline_size_);
831 // Flag set when cache coherency tests need to be run
832 ARG_KVALUE("--cc_test", cc_test_, true);
834 // Set when the cpu_frequency test needs to be run
835 ARG_KVALUE("--cpu_freq_test", cpu_freq_test_, true);
837 // Set the threshold in MHz at which the cpu frequency test will fail.
838 ARG_IVALUE("--cpu_freq_threshold", cpu_freq_threshold_);
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_);
844 // Set number of CPU stress threads.
845 ARG_IVALUE("-C", cpu_stress_threads_);
848 ARG_SVALUE("-l", logfilename_);
851 ARG_IVALUE("-v", verbosity_);
853 // Turn off timestamps logging.
854 ARG_KVALUE("--no_timestamps", log_timestamps_, false);
856 // Set maximum number of errors to collect. Stop running after this many.
857 ARG_IVALUE("--max_errors", max_errorcount_);
859 // Set pattern block size.
860 ARG_IVALUE("-p", page_length_);
862 // Set pattern block size.
863 ARG_IVALUE("--filesize", filesize);
866 ARG_KVALUE("--local_numa", region_mode_, kLocalNuma);
867 ARG_KVALUE("--remote_numa", region_mode_, kRemoteNuma);
869 // Autodetect tempfile locations.
870 ARG_KVALUE("--findfiles", findfiles_, 1);
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;
878 // Stop immediately on any arror, for debugging HW problems.
879 ARG_KVALUE("--stop_on_errors", stop_on_error_, 1);
881 // Don't use internal error polling, allow external detection.
882 ARG_KVALUE("--no_errors", error_poll_, 0);
884 // Never check data as you go.
885 ARG_KVALUE("-F", strict_, 0);
887 // Warm the cpu as you go.
888 ARG_KVALUE("-W", warm_, 1);
890 // Allow runnign on unknown systems with base unimplemented OsLayer
891 ARG_KVALUE("-A", run_on_anything_, 1);
893 // Size of read blocks for disk test.
894 ARG_IVALUE("--read-block-size", read_block_size_);
896 // Size of write blocks for disk test.
897 ARG_IVALUE("--write-block-size", write_block_size_);
899 // Size of segment for disk test.
900 ARG_IVALUE("--segment-size", segment_size_);
902 // Size of disk cache size for disk test.
903 ARG_IVALUE("--cache-size", cache_size_);
905 // Number of blocks to test per segment.
906 ARG_IVALUE("--blocks-per-segment", blocks_per_segment_);
908 // Maximum time a block read should take before warning.
909 ARG_IVALUE("--read-threshold", read_threshold_);
911 // Maximum time a block write should take before warning.
912 ARG_IVALUE("--write-threshold", write_threshold_);
914 // Do not write anything to disk in the disk test.
915 ARG_KVALUE("--destructive", non_destructive_, 0);
917 // Run SAT in monitor mode. No test load at all.
918 ARG_KVALUE("--monitor_mode", monitor_mode_, true);
920 // Run SAT in address mode. Tag all cachelines by virt addr.
921 ARG_KVALUE("--tag_mode", tag_mode_, true);
923 // Dump range map of tested pages..
924 ARG_KVALUE("--do_page_map", do_page_map_, true);
926 // Specify the physical address base to test.
927 ARG_IVALUE("--paddr_base", paddr_base_);
929 // Specify the frequency for power spikes.
930 ARG_IVALUE("--pause_delay", pause_delay_);
932 // Specify the duration of each pause (for power spikes).
933 ARG_IVALUE("--pause_duration", pause_duration_);
936 if (!strcmp(argv[i], "-d")) {
940 diskfilename_.push_back(string(argv[i]));
941 blocktables_.push_back(new DiskBlockTable());
946 // Set number of disk random threads for each disk write thread.
947 ARG_IVALUE("--random-threads", random_threads_);
949 // Set a tempfile to use in a file thread.
950 if (!strcmp(argv[i], "-f")) {
954 filename_.push_back(string(argv[i]));
959 // Set a hostname to use in a network thread.
960 if (!strcmp(argv[i], "-n")) {
964 ipaddrs_.push_back(string(argv[i]));
969 // Run threads that listen for incoming SAT net connections.
970 ARG_KVALUE("--listen", listen_threads_, 1);
972 if (CheckGoogleSpecificArgs(argc, argv, &i)) {
976 ARG_IVALUE("--channel_hash", channel_hash_);
977 ARG_IVALUE("--channel_width", channel_width_);
979 if (!strcmp(argv[i], "--memory_channel")) {
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));
988 channels_.back().push_back(string(channel));
996 if (strcmp(argv[i], "-h") && strcmp(argv[i], "--help")) {
997 printf("\n Unknown argument %s\n", argv[i]);
1001 // Forget it, we printed the help, just bail.
1002 // We don't want to print test status, or any log parser stuff.
1006 Logger::GlobalLogger()->SetVerbosity(verbosity_);
1008 // Update relevant data members with parsed input.
1009 // Translate MB into bytes.
1010 size_ = static_cast<int64>(size_mb_) * kMegabyte;
1012 // Set logfile flag.
1013 if (strcmp(logfilename_, ""))
1015 // Checks valid 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_);
1023 // Revert to default page length.
1024 logprintf(6, "Process Error: "
1025 "Invalid page size %d\n", page_length_);
1026 page_length_ = kSatPageSize;
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)
1038 // Validate memory channel parameters if supplied
1039 if (channels_.size()) {
1040 if (channels_.size() == 1) {
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");
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);
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");
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_);
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());
1079 // Print each argument.
1080 for (int i = 0; i < argc; i++) {
1083 cmdline_ += argv[i];
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 "
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");
1165 bool Sat::CheckGoogleSpecificArgs(int argc, char **argv, int *i) {
1166 // Do nothing, no google-specific argument on public stressapptest
1170 void Sat::GoogleOsOptions(std::map<std::string, std::string> *options) {
1171 // Do nothing, no OS-specific argument on public stressapptest
1174 // Launch the SAT task threads. Returns 0 on error.
1175 void Sat::InitializeThreads() {
1176 // Memory copy threads.
1177 AcquireWorkerLock();
1179 logprintf(12, "Log: Starting worker threads\n");
1180 WorkerVector *memory_vector = new WorkerVector();
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();
1187 ErrorPollThread *thread = new ErrorPollThread();
1188 thread->InitThread(total_threads_++, this, os_, patternlist_,
1189 &continuous_status_);
1191 error_vector->insert(error_vector->end(), thread);
1193 logprintf(5, "Log: Skipping error poll thread due to --no_errors flag\n");
1195 workers_map_.insert(make_pair(kErrorType, error_vector));
1197 // Only start error poll threads for monitor-mode SAT,
1198 // skip all other types of worker threads.
1199 if (monitor_mode_) {
1200 ReleaseWorkerLock();
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_);
1209 if ((region_count_ > 1) && (region_mode_)) {
1210 int32 region = region_find(i % region_count_);
1211 cpu_set_t *cpuset = os_->FindCoreMask(region);
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));
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.
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());
1244 // Set thread affinity.
1245 thread->set_cpu_mask_to_cpu(nthbit);
1248 memory_vector->insert(memory_vector->end(), thread);
1250 workers_map_.insert(make_pair(kMemoryType, memory_vector));
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);
1263 fileio_vector->insert(fileio_vector->end(), thread);
1265 workers_map_.insert(make_pair(kFileIOType, fileio_vector));
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_);
1276 netslave_vector->insert(netslave_vector->end(), thread);
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());
1284 netio_vector->insert(netio_vector->end(), thread);
1286 workers_map_.insert(make_pair(kNetIOType, netio_vector));
1287 workers_map_.insert(make_pair(kNetSlaveType, netslave_vector));
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_);
1296 check_vector->insert(check_vector->end(), thread);
1298 workers_map_.insert(make_pair(kCheckType, check_vector));
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_);
1308 invert_vector->insert(invert_vector->end(), thread);
1310 workers_map_.insert(make_pair(kInvertType, invert_vector));
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);
1329 logprintf(12, "Log: DiskThread::SetParameters() failed\n");
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);
1346 logprintf(12, "Log: RandomDiskThread::SetParameters() failed\n");
1352 workers_map_.insert(make_pair(kDiskType, disk_vector));
1353 workers_map_.insert(make_pair(kRandomDiskType, random_vector));
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_);
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());
1383 // Set thread affinity.
1384 thread->set_cpu_mask_to_cpu(nthbit);
1388 cpu_vector->insert(cpu_vector->end(), thread);
1390 workers_map_.insert(make_pair(kCPUType, cpu_vector));
1392 // CPU Cache Coherency Threads - one for each core available.
1394 WorkerVector *cc_vector = new WorkerVector();
1395 logprintf(12, "Log: Starting cpu cache coherency threads\n");
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);
1402 // Initialize the strucutre.
1403 memset(cc_cacheline_data_, 0,
1404 sizeof(cc_cacheline_data) * cc_cacheline_count_);
1406 int num_cpus = CpuCount();
1408 // Calculate the number of cache lines needed just to give each core
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);
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_);
1428 num = reinterpret_cast<int*>(memalign(
1429 line_size, line_size * needed_lines * cc_cacheline_count_));
1430 int err_result = (num == 0);
1432 sat_assert(err_result == 0);
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);
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);
1451 // Insert the thread into the vector.
1452 cc_vector->insert(cc_vector->end(), thread);
1454 workers_map_.insert(make_pair(kCCType, cc_vector));
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_,
1463 // This thread should be paused when other threads are paused.
1464 thread->InitThread(total_threads_++, this, os_, NULL,
1465 &power_spike_status_);
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));
1472 ReleaseWorkerLock();
1475 // Return the number of cpus actually present in the machine.
1476 int Sat::CpuCount() {
1477 return sysconf(_SC_NPROCESSORS_CONF);
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;
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();
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();
1508 ReleaseWorkerLock();
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;
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);
1530 reap_check_status.Initialize();
1531 // Check threads should be marked to stop ASAP.
1532 reap_check_status.StopWorkers();
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();
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();
1548 // Reap all children. Stopped threads should have already ended.
1549 // Result checking threads will end when they have finished
1551 logprintf(12, "Log: Join all outstanding threads\n");
1554 errorcount_ = GetTotalErrorCount();
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 "
1565 (*it)->ThreadID(), (*it)->GetStatus(),
1566 (*it)->GetRunDurationUSec()*1.0/1000000);
1570 if ((*it)->GetErrorCount())
1572 logprintf(priority, "Log: Thread %d found %lld hardware incidents\n",
1573 (*it)->ThreadID(), (*it)->GetErrorCount());
1576 ReleaseWorkerLock();
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 "
1586 (*it)->ThreadID(), (*it)->GetStatus(),
1587 (*it)->GetRunDurationUSec()*1.0/1000000);
1590 errorcount_ += (*it)->GetErrorCount();
1592 if ((*it)->GetErrorCount())
1594 logprintf(priority, "Log: Thread %d found %lld hardware incidents\n",
1595 (*it)->ThreadID(), (*it)->GetErrorCount());
1598 reap_check_vector.clear();
1599 reap_check_status.Destroy();
1602 // Print queuing information.
1603 void Sat::QueueStats() {
1604 finelock_q_->QueueAnalysis();
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.;
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;
1626 total_bandwidth = total_data / max_runtime_sec;
1628 logprintf(0, "Stats: Completed: %.2fM in %.2fs %.2fMB/s, "
1629 "with %d hardware incidents, %d errors\n",
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();
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();
1656 GoogleMemoryStats(&memcopy_data, &memcopy_bandwidth);
1657 logprintf(4, "Stats: Memory Copy: %.2fM at %.2fMB/s\n",
1662 void Sat::GoogleMemoryStats(float *memcopy_data,
1663 float *memcopy_bandwidth) {
1664 // Do nothing, should be implemented by subclasses.
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();
1678 logprintf(4, "Stats: File Copy: %.2fM at %.2fMB/s\n",
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();
1694 logprintf(4, "Stats: Data Check: %.2fM at %.2fMB/s\n",
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();
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();
1718 logprintf(4, "Stats: Net Copy: %.2fM at %.2fMB/s\n",
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();
1734 logprintf(4, "Stats: Invert Data: %.2fM at %.2fMB/s\n",
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();
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();
1759 logprintf(4, "Stats: Disk: %.2fM at %.2fMB/s\n",
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() {
1776 // Get total error count, summing across all threads..
1777 int64 Sat::GetTotalErrorCount() {
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();
1788 ReleaseWorkerLock();
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();
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());
1818 delete map_it->second;
1820 workers_map_.clear();
1821 logprintf(12, "Log: Destroying WorkerStatus objects\n");
1822 power_spike_status_.Destroy();
1823 continuous_status_.Destroy();
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.
1830 // Using frequencies instead of intervals with their accompanying drift allows
1831 // users to better predict when the actions will occur throughout a run.
1834 // frequency: seconds
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);
1844 // Run the actual test.
1846 // Install signal handlers to gracefully exit in the middle of a run.
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.
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.
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.
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);
1875 // Kick off all the worker threads.
1876 logprintf(12, "Log: Launching worker threads\n");
1877 InitializeThreads();
1879 pthread_sigmask(SIG_SETMASK, &prev_blocked_signals, NULL);
1881 logprintf(12, "Log: Starting countdown with %d seconds\n", runtime_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;
1890 const time_t start = time(NULL);
1891 const time_t end = start + runtime_seconds_;
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;
1904 // This is an int because it's for logprintf().
1905 const int seconds_remaining = end - now;
1908 // Handle early exit.
1909 logprintf(0, "Log: User exiting early (%d seconds remaining)\n",
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",
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);
1932 if (next_injection && now >= next_injection) {
1934 logprintf(4, "Log: Injecting error (%d seconds remaining)\n",
1936 struct page_entry src;
1938 src.pattern = patternlist_->GetPattern(0);
1940 next_injection = NextOccurance(kInjectionFrequency, start, now);
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");
1950 next_resume = now + pause_duration_;
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);
1963 sat_sleep(NextOccurance(kSleepFrequency, start, now) - now);
1969 logprintf(0, "Stats: Found %lld hardware incidents\n", errorcount_);
1976 logprintf(12, "Log: Uninstalling signal handlers\n");
1977 signal(SIGINT, prev_sigint_handler);
1978 signal(SIGTERM, prev_sigterm_handler);
1983 // Clean up all resources.
1984 bool Sat::Cleanup() {
1986 Logger::GlobalLogger()->StopThread();
1987 Logger::GlobalLogger()->SetStdoutOnly();
1993 patternlist_->Destroy();
1994 delete patternlist_;
2015 delete[] page_bitmap_;
2018 for (size_t i = 0; i < blocktables_.size(); i++) {
2019 delete blocktables_[i];
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);
2033 free(cc_cacheline_data_);
2036 sat_assert(0 == pthread_mutex_destroy(&worker_lock_));
2042 // Pretty print really obvious results.
2043 bool Sat::PrintResults() {
2048 logprintf(4, "Status: FAIL - test encountered procedural errors\n");
2050 } else if (errorcount_) {
2051 logprintf(4, "Status: FAIL - test discovered HW problems\n");
2054 logprintf(4, "Status: PASS - please verify no corrected errors\n");
2061 // Helper functions.
2062 void Sat::AcquireWorkerLock() {
2063 sat_assert(0 == pthread_mutex_lock(&worker_lock_));
2065 void Sat::ReleaseWorkerLock() {
2066 sat_assert(0 == pthread_mutex_unlock(&worker_lock_));
2069 void logprintf(int priority, const char *format, ...) {
2071 va_start(args, format);
2072 Logger::GlobalLogger()->VLogF(priority, format, args);
2076 // Stop the logging thread and verify any pending data is written to the log.
2078 Logger::GlobalLogger()->StopThread();