pytorch/caffe2/utils/bench_utils.cc
Nikita Shulga a9b0a921d5 Disable avoid-non-const-global-variables lint check (#62008)
Summary:
As GoogleTest `TEST` macro is non-compliant with it as well as `DEFINE_DISPATCH`

All changes but the ones to `.clang-tidy` are generated using following script:
```
for i in `find . -type f -iname "*.c*" -or -iname "*.h"|xargs grep cppcoreguidelines-avoid-non-const-global-variables|cut -f1 -d:|sort|uniq`;  do sed -i "/\/\/ NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)/d" $i; done
```

Pull Request resolved: https://github.com/pytorch/pytorch/pull/62008

Reviewed By: driazati, r-barnes

Differential Revision: D29838584

Pulled By: malfet

fbshipit-source-id: 1b2f8602c945bd4ce50a9bfdd204755556e31d13
2021-07-22 18:04:40 -07:00

94 lines
3.0 KiB
C++

#include <cpuinfo.h>
// NOLINTNEXTLINE(modernize-deprecated-headers)
#include <stdint.h>
// NOLINTNEXTLINE(modernize-deprecated-headers)
#include <stdlib.h>
#include "caffe2/core/logging.h"
#include "caffe2/utils/bench_utils.h"
namespace caffe2 {
uint32_t wipe_cache() {
static uint32_t* wipe_buffer = nullptr;
static size_t wipe_size = 0;
if (wipe_buffer == nullptr) {
CAFFE_ENFORCE(cpuinfo_initialize(), "failed to initialize cpuinfo");
const cpuinfo_processor* processor = cpuinfo_get_processor(0);
if (processor->cache.l4 != nullptr) {
wipe_size = processor->cache.l4->size;
} else if (processor->cache.l3 != nullptr) {
wipe_size = processor->cache.l3->size;
} else if (processor->cache.l2 != nullptr) {
wipe_size = processor->cache.l2->size;
} else {
wipe_size = processor->cache.l1d->size;
}
#if CPUINFO_ARCH_ARM || CPUINFO_ARCH_ARM64
/*
* On ARM precise cache size is not available, and cpuinfo may
* underestimate. Use max for uArch (see src/arm/cache.c)
*/
switch (processor->core->uarch) {
case cpuinfo_uarch_cortex_a5:
wipe_size = 512 * 1024; /* Max observed */
break;
case cpuinfo_uarch_cortex_a7:
wipe_size = 1024 * 1024; /* uArch max */
break;
case cpuinfo_uarch_cortex_a8:
wipe_size = 1024 * 1024; /* uArch max */
break;
case cpuinfo_uarch_cortex_a9:
wipe_size = 1024 * 1024; /* Max observed */
break;
case cpuinfo_uarch_cortex_a12:
case cpuinfo_uarch_cortex_a17:
wipe_size = 8 * 1024 * 1024; /* uArch max */
break;
case cpuinfo_uarch_cortex_a15:
wipe_size = 4 * 1024 * 1024; /* uArch max */
break;
case cpuinfo_uarch_cortex_a35:
wipe_size = 1024 * 1024; /* uArch max */
break;
case cpuinfo_uarch_cortex_a53:
wipe_size = 2 * 1024 * 1024; /* uArch max */
break;
case cpuinfo_uarch_cortex_a57:
wipe_size = 2 * 1024 * 1024; /* uArch max */
break;
case cpuinfo_uarch_cortex_a72:
wipe_size = 4 * 1024 * 1024; /* uArch max */
break;
case cpuinfo_uarch_cortex_a73:
wipe_size = 8 * 1024 * 1024; /* uArch max */
break;
case cpuinfo_uarch_cortex_a55:
case cpuinfo_uarch_cortex_a75:
case cpuinfo_uarch_meerkat_m3:
wipe_size = 4 * 1024 * 1024; /* DynamIQ max */
break;
default:
wipe_size = 60 * 1024 * 1024;
break;
}
#endif
LOG(INFO) << "Allocating cache wipe buffer of size " << wipe_size;
// NOLINTNEXTLINE(cppcoreguidelines-no-malloc)
wipe_buffer = static_cast<uint32_t*>(malloc(wipe_size));
CAFFE_ENFORCE(wipe_buffer != nullptr);
}
uint32_t hash = 0;
for (uint32_t i = 0; i * sizeof(uint32_t) < wipe_size; i += 8) {
// NOLINTNEXTLINE(clang-analyzer-core.uninitialized.Assign)
hash ^= wipe_buffer[i];
wipe_buffer[i] = hash;
}
/* Make sure compiler doesn't optimize the loop away */
return hash;
}
} /* namespace caffe2 */