Skip to content

Instantly share code, notes, and snippets.

@ywkaras
Created June 3, 2020 16:00
Show Gist options
  • Save ywkaras/2ab87fc4096c0921747250316485d23d to your computer and use it in GitHub Desktop.
Save ywkaras/2ab87fc4096c0921747250316485d23d to your computer and use it in GitHub Desktop.
TWATCH3
[root@atslab03 TWATCH3]# cat x.cc
#include <iostream>
#include <cstdlib>
#include <atomic>
#include <cstdint>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
void check(bool expr, int line)
{
if (!expr) {
std::cerr << "FAILED on line " << line << std::endl;
std::exit(1);
}
}
#define CHECK(EXPR) check((EXPR), __LINE__)
std::atomic<int> flag{0};
int const Num_low_priority_threads = 10;
unsigned volatile i[Num_low_priority_threads];
void *tfunc(void *idx)
{
unsigned volatile *pi = i + reinterpret_cast<std::uintptr_t>(idx);
while (!flag) {
;
}
while (true) {
++*pi;
}
return nullptr;
}
// Note: The intention is that this thread is always running when the process is running, so this program will
// only work on a multi-core CPU.
//
void *t_mon_func(void *)
{
while (!flag) {
;
}
std::abort();
return nullptr;
}
pthread_t thread[Num_low_priority_threads], mon_thread;
int main()
{
pthread_attr_t pt_attr;
CHECK(pthread_attr_init(&pt_attr) == 0);
CHECK(pthread_attr_setscope(&pt_attr, PTHREAD_SCOPE_SYSTEM) == 0);
for (std::uintptr_t j = 0; j < Num_low_priority_threads; ++j) {
CHECK(pthread_create(thread + j, &pt_attr, tfunc, reinterpret_cast<void *>(j)) == 0);
}
#if 0
#define SCHED_ SCHED_FIFO
#else
#define SCHED_ SCHED_RR
#endif
sched_param sparam;
sparam.sched_priority = sched_get_priority_max(SCHED_);
CHECK(pthread_create(&mon_thread, &pt_attr, t_mon_func, nullptr) == 0);
CHECK(pthread_setschedparam(mon_thread, SCHED_, &sparam) == 0);
CHECK(pthread_attr_destroy(&pt_attr) == 0);
int policy;
CHECK(pthread_getschedparam(mon_thread, &policy, &sparam) == 0);
std::cerr << "policy=" << policy <<std::endl;
CHECK(policy == SCHED_);
CHECK(sparam.sched_priority == sched_get_priority_max(SCHED_));
sleep(1);
flag = 1;
while (true) {
;
}
return 0;
}
[root@atslab03 TWATCH3]# cat x.sh
#!/bin/bash
# NOTE: Must be root to run this. Probably will have to run 'echo -1 > /proc/sys/kernel/sched_rt_runtime_us'
# for the pthread_setschedparam() to work.
ulimit -c unlimited
rm -f watch_test
GCC=/opt/rh/devtoolset-8/root/bin/x86_64-redhat-linux-gcc-8
$GCC -g -O3 -std=c++17 x.cc -o watch_test -lstdc++ -lpthread
./watch_test
[root@atslab03 TWATCH3]# . x.sh
policy=2
Aborted (core dumped)
[root@atslab03 TWATCH3]# gdb watch_test core.4024
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-119.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/wkaras/REPOS/OS_MISC/TWATCH3/watch_test...done.
[New LWP 4035]
[New LWP 4024]
[New LWP 4034]
[New LWP 4026]
[New LWP 4025]
[New LWP 4028]
[New LWP 4029]
[New LWP 4027]
[New LWP 4030]
[New LWP 4031]
[New LWP 4032]
[New LWP 4033]
...
Core was generated by `./watch_test'.
Program terminated with signal 6, Aborted.
#0 0x00007f0db4050387 in raise () from /lib64/libc.so.6
Missing separate debuginfos, use: debuginfo-install glibc-2.17-307.el7.1.x86_64
(gdb) p i
$1 = {9898, 11078, 10952, 9091, 10309, 11840, 12182, 10239, 11605, 10623}
(gdb) q
[root@atslab03 TWATCH3]#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment