This patch appears to be corrupt - the line "int tests = 0, fails = 0;" is
missing at least a space - and neither "git am" nor "patch" want to accept
it.
I don't want to try to fix it without knowing what else I might be missing
- so can you please resend it? Thanks.

--
Nadav Har'El
n...@scylladb.com


On Fri, Nov 19, 2021 at 6:25 AM Waldemar Kozaczuk <jwkozac...@gmail.com>
wrote:

> This patch replaces usage of various OSv specific symbols with glibc
> and STD C++ ones like:
> - debug() with standard printf(),
> - sched::thread with std::thread,
> - sched::thread::current()->get_cpu()->id with sched_currentcpu(),
> - sched::cpus.size() with get_nprocs().
>
> These changes make these 5 unit tests compilable
> and runnable on Linux and allows for verification if these tests
> pass on Linux.
>
> Signed-off-by: Waldemar Kozaczuk <jwkozac...@gmail.com>
> ---
>  include/api/sched.h                   |   1 +
>  tests/tst-af-local.cc                 |  24 ++---
>  tests/tst-bsd-tcp1.cc                 |  91 +++++++++---------
>  tests/tst-pthread-affinity-inherit.cc | 127 +++++++++++++-------------
>  tests/tst-pthread-affinity.cc         |  36 ++++----
>  tests/tst-yield.cc                    |  15 ++-
>  6 files changed, 147 insertions(+), 147 deletions(-)
>
> diff --git a/include/api/sched.h b/include/api/sched.h
> index 925ea986..1893efc1 100644
> --- a/include/api/sched.h
> +++ b/include/api/sched.h
> @@ -127,6 +127,7 @@ inline static size_t CPU_ALLOC_SIZE(size_t count) {
>      return ((count + __NCPUBITS - 1) / __NCPUBITS) * sizeof (__cpu_mask);
>  }
>
> +int sched_getcpu(void);
>  int sched_getaffinity(pid_t pid, size_t cpusetsize,
>                               cpu_set_t *mask);
>
> diff --git a/tests/tst-af-local.cc b/tests/tst-af-local.cc
> index aacb6287..7d79a275 100644
> --- a/tests/tst-af-local.cc
> +++ b/tests/tst-af-local.cc
> @@ -8,8 +8,10 @@
>  #include <sys/socket.h>
>  #include <sys/poll.h>
>  #include <unistd.h>
> -#include <osv/sched.hh>
> -#include <osv/debug.hh>
> +#include <cstdio>
> +#include <cstring>
> +#include <thread>
> +#
> - sched::thread with std::thread,
> - include <vector>
>
>  int tests = 0, fails = 0;
>
> @@ -17,7 +19,7 @@ static void report(bool ok, const char* msg)
>  {
>      ++tests;
>      fails += !ok;
> -    debug("%s: %s\n", (ok ? "PASS" : "FAIL"), msg);
> +    printf("%s: %s\n", (ok ? "PASS" : "FAIL"), msg);
>  }
>
>  int main(int ac, char** av)
> @@ -36,13 +38,12 @@ int main(int ac, char** av)
>      memcpy(msg, "snafu", 5);
>      memset(reply, 0, 5);
>      int r2;
> -    std::unique_ptr<sched::thread> t1(sched::thread::make([&] {
> +    std::thread t1([&] {
>          r2 = read(s[1], reply, 5);
> -    }));
> -    t1->start();
> +    });
>      sleep(1);
>      r = write(s[0], msg, 5);
> -    t1->join();
> +    t1.join();
>      report(r2 == 5 && memcmp(msg, reply, 5) == 0, "read before write");
>
>      memcpy(msg, "fooba", 5);
> @@ -60,17 +61,16 @@ int main(int ac, char** av)
>
>      memcpy(msg, "smeg!", 5);
>      memset(reply, 0, 5);
> -    std::unique_ptr<sched::thread> t2(sched::thread::make([&] {
> +    std::thread t2([&] {
>          poller.revents = 0;
>          r2 = poll(&poller, 1, 5000);
>          report(r2 == 1 && poller.revents == POLLIN, "waiting poll");
>          r2 = read(s[1], reply, 5);
>          report(r2 == 5 && memcmp(msg, reply, 5) == 0, "read after waiting
> poll");
> -    }));
> -    t2->start();
> +    });
>      sleep(1);
>      r = write(s[0], msg, 5);
> -    t2->join();
> +    t2.join();
>      report(r == 5, "write to polling socket");
>
>      close(s[1]);
> @@ -132,7 +132,7 @@ int main(int ac, char** av)
>      report(nsock > 100, "create many sockets");
>
>
> -    debug("SUMMARY: %d tests, %d failures\n", tests, fails);
> +    printf("SUMMARY: %d tests, %d failures\n", tests, fails);
>  }
>
>
> diff --git a/tests/tst-bsd-tcp1.cc b/tests/tst-bsd-tcp1.cc
> index 884500bb..f20e588b 100644
> --- a/tests/tst-bsd-tcp1.cc
> +++ b/tests/tst-bsd-tcp1.cc
> @@ -9,18 +9,21 @@
>
>  #include <boost/test/unit_test.hpp>
>
> -#include <osv/sched.hh>
> -
>  #include <sys/socket.h>
>  #include <netinet/in.h>
>  #include <arpa/inet.h>
> +#include <cstdio>
> +#include <thread>
>
> -#include <osv/debug.hh>
>  #include "tst-hub.hh"
>
>  #include <boost/asio.hpp>
>
> -#define dbg_d(...) tprintf_d("tst-tcp1", __VA_ARGS__)
> +#if CONF_logger_debug
> +    #define dbg_d(tag, ...) printf(__VA_ARGS__)
> +#else
> +    #define dbg_d(tag, ...) do{}while(0)
> +#endif
>
>  #define LISTEN_PORT (5555)
>  #define ITERATIONS (400)
> @@ -42,10 +45,10 @@ public:
>          int i;
>
>          /* Create listening socket */
> -        dbg_d("server: creating listening socket...");
> +        dbg_d("tst-tcp1: server: creating listening socket...");
>          listen_s = socket(AF_INET, SOCK_STREAM, 0);
>          if (listen_s < 0) {
> -            dbg_d("server: socket() failed!");
> +            dbg_d("tst-tcp1: server: socket() failed!");
>              return -1;
>          }
>
> @@ -54,7 +57,7 @@ public:
>          error = setsockopt(listen_s, SOL_SOCKET, SO_REUSEADDR, &optval,
>              sizeof(optval));
>          if (error < 0) {
> -            dbg_d("server: setsockopt(SO_REUSEADDR) failed, error=%d",
> error);
> +            dbg_d("tst-tcp1: server: setsockopt(SO_REUSEADDR) failed,
> error=%d", error);
>              return -1;
>          }
>
> @@ -65,63 +68,63 @@ public:
>          inet_aton("127.0.0.1", &laddr.sin_addr);
>          laddr.sin_port = htons(LISTEN_PORT);
>
> -        dbg_d("server: calling bind()");
> +        dbg_d("tst-tcp1: server: calling bind()");
>          if ( bind(listen_s, (struct sockaddr *) &laddr, sizeof(laddr)) <
> 0 ) {
> -            dbg_d("server: bind() failed!");
> +            dbg_d("tst-tcp1: server: bind() failed!");
>              return -1;
>          }
>
>          /* Listen */
> -        dbg_d("server: calling listen()");
> +        dbg_d("tst-tcp1: server: calling listen()");
>          if ( listen(listen_s, 256) < 0 ) {
> -            dbg_d("server: listen() failed!");
> +            dbg_d("tst-tcp1: server: listen() failed!");
>              return -1;
>          }
>
>          for (i=0; i < ITERATIONS; i++) {
>
>              /*  Wait for a connection*/
> -            dbg_d("server: calling accept()");
> +            dbg_d("tst-tcp1: server: calling accept()");
>              if ( (client_s = accept(listen_s, NULL, NULL) ) < 0 ) {
> -                dbg_d("server: accept() failed!");
> +                dbg_d("tst-tcp1: server: accept() failed!");
>                  return -1;
>              }
>
> -            dbg_d("server: got a connection!");
> +            dbg_d("tst-tcp1: server: got a connection!");
>
>              /* Read max buf_size bytes */
>              int bytes = read(client_s, &buf, buf_size);
>              if (bytes < 0) {
> -                dbg_d("server: read() failed!");
> +                dbg_d("tst-tcp1: server: read() failed!");
>                  close(client_s);
>                  return -1;
>              }
>
>              /* Echo back */
> -            dbg_d("Echoing %d bytes", bytes);
> +            dbg_d("tst-tcp1: Echoing %d bytes", bytes);
>
>              int bytes2 = write(client_s, &buf, bytes);
>              if (bytes2 < 0) {
> -                dbg_d("server: write() failed!");
> +                dbg_d("tst-tcp1: server: write() failed!");
>                  close(client_s);
>                  return -1;
>              }
>
> -            dbg_d("server: echoed %d bytes", bytes2);
> -            dbg_d("server: calling close()", bytes2);
> +            dbg_d("tst-tcp1: server: echoed %d bytes", bytes2);
> +            dbg_d("tst-tcp1: server: calling close()", bytes2);
>
>              if ( close(client_s) < 0 ) {
> -                dbg_d("server: close() failed!");
> +                dbg_d("tst-tcp1: server: close() failed!");
>                  return -1;
>              }
>          }
>
>          if ( close(listen_s) < 0 ) {
> -            dbg_d("server: close() failed!");
> +            dbg_d("tst-tcp1: server: close() failed!");
>              return -1;
>          }
>
> -        dbg_d("server: DONE");
> +        dbg_d("tst-tcp1: server: DONE");
>          return 0;
>      }
>
> @@ -138,43 +141,43 @@ public:
>
>          for (int i=0; i < ITERATIONS; i++) {
>
> -            dbg_d("client: creating socket()... #%d", i+1);
> +            dbg_d("tst-tcp1: client: creating socket()... #%d", i+1);
>              int s = socket(AF_INET, SOCK_STREAM, 0);
>              if (s < 0) {
> -                dbg_d("client: socket() failed!");
> +                dbg_d("tst-tcp1: client: socket() failed!");
>                  return -1;
>              }
>
> -            dbg_d("client: connecting to 127.0.0.1...");
> +            dbg_d("tst-tcp1: client: connecting to 127.0.0.1...");
>              if ( connect(s, (struct sockaddr *)&raddr, sizeof(raddr)) < 0
> ) {
> -                dbg_d("client: connect() failed!");
> +                dbg_d("tst-tcp1: client: connect() failed!");
>                  return -1;
>              }
>
> -            dbg_d("client: writing message: %s", message);
> +            dbg_d("tst-tcp1: client: writing message: %s", message);
>              int bytes = write(s, message, strlen(message) + 1);
>              if (bytes < 0) {
> -                dbg_d("client: write() failed!");
> +                dbg_d("tst-tcp1: client: write() failed!");
>                  return -1;
>              }
>
>              int bytes2 = read(s, &replay, buf_size);
>              if (bytes2 < 0) {
> -                 dbg_d("client: read() failed!");
> +                 dbg_d("tst-tcp1: client: read() failed!");
>                   return -1;
>               }
>
>              replay[bytes2] = '\0';
> -            dbg_d("client: got replay: %s", replay);
> +            dbg_d("tst-tcp1: client: got replay: %s", replay);
>
> -            dbg_d("client: closing socket()");
> +            dbg_d("tst-tcp1: client: closing socket()");
>              if ( close(s) < 0 ) {
> -                dbg_d("client: close() failed!");
> +                dbg_d("tst-tcp1: client: close() failed!");
>                  return -1;
>              }
>          }
>
> -        dbg_d("client: DONE");
> +        dbg_d("tst-tcp1: client: DONE");
>          return 0;
>      }
>
> @@ -183,21 +186,16 @@ public:
>          _client_result = 0;
>          _server_result = 0;
>
> -        sched::thread *srv = sched::thread::make([&] {
> +        std::thread srv([&] {
>              _server_result = tcp_server();
>          });
> -        sched::thread *cli = sched::thread::make([&] {
> +        sleep(1);
> +        std::thread cli([&] {
>              _client_result = tcp_client();
>          });
>
> -        srv->start();
> -        sleep(1);
> -        cli->start();
> -
> -        cli->join();
> -        srv->join();
> -        delete(cli);
> -        delete(srv);
> +        cli.join();
> +        srv.join();
>
>          return (_client_result + _server_result);
>      }
> @@ -209,15 +207,15 @@ private:
>
>  BOOST_AUTO_TEST_CASE(test_tcp_client_server)
>  {
> -    dbg_d("BSD TCP1 Test - Begin");
> +    dbg_d("tst-tcp1: BSD TCP1 Test - Begin");
>
>      test_bsd_tcp1 tcp1;
>      int rc = tcp1.run();
>
>      BOOST_REQUIRE(rc >= 0);
>
> -    dbg_d("BSD TCP1 Test completed: %s!", rc >= 0 ? "PASS" : "FAIL");
> -    dbg_d("BSD TCP1 Test - End");
> +    dbg_d("tst-tcp1: BSD TCP1 Test completed: %s!", rc >= 0 ? "PASS" :
> "FAIL");
> +    dbg_d("tst-tcp1: BSD TCP1 Test - End");
>  }
>
>
> @@ -235,4 +233,3 @@ BOOST_AUTO_TEST_CASE(test_shutdown_wr)
>
>  #undef ITERATIONS
>  #undef LISTEN_PORT
> -#undef dbg_d
> diff --git a/tests/tst-pthread-affinity-inherit.cc
> b/tests/tst-pthread-affinity-inherit.cc
> index d72906ea..65b44edd 100644
> --- a/tests/tst-pthread-affinity-inherit.cc
> +++ b/tests/tst-pthread-affinity-inherit.cc
> @@ -8,9 +8,10 @@
>  #include <stdbool.h>
>  #include <sys/time.h>
>  #include <errno.h>
> -#include <osv/sched.hh>
> +#include <sched.h>
>  #include <unistd.h>
>  #include <atomic>
> +#include <sys/sysinfo.h>
>
>  static std::atomic<unsigned> tests_total(0), tests_failed(0);
>
> @@ -26,22 +27,22 @@ void *check_cpuset(void *ref_cs2)
>  {
>      const cpu_set_t *ref_cs = static_cast<cpu_set_t *>(ref_cs2);
>      cpu_set_t cs;
> -    unsigned int cpu_id;
> +    int cpu_id;
>      bool success;
>
>      sched_getaffinity(0, sizeof(cs), &cs);
>      success = true;
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          success = success && (CPU_ISSET(i, &cs) == CPU_ISSET(i, ref_cs));
>      }
>      report("child pinning is equal to reference cpuset", success);
>
> -    cpu_id = sched::thread::current()->get_cpu()->id;
> +    cpu_id = sched_getcpu();
>      report("started on the correct CPU", CPU_ISSET(cpu_id, ref_cs));
>
>      usleep(1000);
>
> -    cpu_id = sched::thread::current()->get_cpu()->id;
> +    cpu_id = sched_getcpu();
>      report("re-scheduled on the correct CPU", CPU_ISSET(cpu_id, ref_cs));
>
>      return NULL;
> @@ -62,10 +63,10 @@ int main(void)
>      // we are started from httpserver, and should be unpinned.
>      CPU_ZERO(&cs);
>      sched_getaffinity(0, sizeof(cs), &cs);
> -    success = CPU_COUNT(&cs) == (int)sched::cpus.size();
> -    report("main is pinned to exactly sched::cpus.size cpus", success);
> +    success = CPU_COUNT(&cs) == get_nprocs();
> +    report("main is pinned to exactly get_nprocs cpus", success);
>      success = true;
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          success = success && CPU_ISSET(i, &cs);
>      }
>      report("main is pinned to all cpus", success);
> @@ -73,10 +74,10 @@ int main(void)
>      // new threads should be unpinned, as cpuset requests unpinned thread
>      printf("new threads from unpinned parent sequential test:\n");
>      CPU_ZERO(&cs);
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_SET(i, &cs);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_attr_init(&attr);
>          pthread_attr_setaffinity_np(&attr, sizeof(cs), &cs);
>          pthread_create(&thread, &attr, check_cpuset, &cs);
> @@ -87,10 +88,10 @@ int main(void)
>      // new threads should be unpinned, because parent is unpinned
>      printf("new threads from unpinned parent sequential test (attr with
> unset cpuset):\n");
>      CPU_ZERO(&cs);
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_SET(i, &cs);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_attr_init(&attr);
>          pthread_create(&thread, &attr, check_cpuset, &cs);
>          pthread_join(thread, NULL);
> @@ -100,69 +101,69 @@ int main(void)
>      // new threads should be unpinned, because parent is unpinned
>      printf("new threads from unpinned parent sequential test
> (attr=NULL):\n");
>      CPU_ZERO(&cs);
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_SET(i, &cs);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_create(&thread, NULL, check_cpuset, &cs);
>          pthread_join(thread, NULL);
>      }
>
>      printf("new threads from unpinned parent parallel test:\n");
> -    threads = new pthread_t[sched::cpus.size()];
> -    attrs = new pthread_attr_t[sched::cpus.size()];
> +    threads = new pthread_t[get_nprocs()];
> +    attrs = new pthread_attr_t[get_nprocs()];
>      CPU_ZERO(&cs);
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_SET(i, &cs);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_attr_init(&attrs[i]);
>          pthread_attr_setaffinity_np(&attrs[i], sizeof(cs), &cs);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_create(&threads[i], &attrs[i], check_cpuset, &cs);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_join(threads[i], NULL);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_attr_destroy(&attrs[i]);
>      }
>      delete[] attrs;
>      delete[] threads;
>
>      printf("new threads from unpinned parent parallel test (attr with
> unset cpuset):\n");
> -    threads = new pthread_t[sched::cpus.size()];
> -    attrs = new pthread_attr_t[sched::cpus.size()];
> +    threads = new pthread_t[get_nprocs()];
> +    attrs = new pthread_attr_t[get_nprocs()];
>      CPU_ZERO(&cs);
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_SET(i, &cs);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_attr_init(&attrs[i]);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_create(&threads[i], &attrs[i], check_cpuset, &cs);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_join(threads[i], NULL);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_attr_destroy(&attrs[i]);
>      }
>      delete[] attrs;
>      delete[] threads;
>
>      printf("new threads from unpinned parent parallel test
> (attr=NULL):\n");
> -    threads = new pthread_t[sched::cpus.size()];
> +    threads = new pthread_t[get_nprocs()];
>      CPU_ZERO(&cs);
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_SET(i, &cs);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_create(&threads[i], NULL, check_cpuset, &cs);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_join(threads[i], NULL);
>      }
>      delete[] threads;
> @@ -170,7 +171,7 @@ int main(void)
>
>      // new threads should be pinned, as cpuset requests pinned thread
>      printf("new threads from pinned parent sequential test:\n");
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_ZERO(&cs);
>          CPU_SET(i, &cs);
>          pthread_setaffinity_np(pthread_self(), sizeof(cs), &cs);
> @@ -182,12 +183,12 @@ int main(void)
>      }
>      // new thread on different CPU
>      printf("new threads from pinned parent sequential test (cpuset for
> different cpu):\n");
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_ZERO(&cs);
>          CPU_SET(i, &cs);
>          pthread_setaffinity_np(pthread_self(), sizeof(cs), &cs);
>          CPU_ZERO(&ref_cs);
> -        CPU_SET((i + 2) % sched::cpus.size(), &ref_cs);
> +        CPU_SET((i + 2) % get_nprocs(), &ref_cs);
>          pthread_attr_init(&attr);
>          pthread_attr_setaffinity_np(&attr, sizeof(ref_cs), &ref_cs);
>          pthread_create(&thread, &attr, check_cpuset, &ref_cs);
> @@ -197,7 +198,7 @@ int main(void)
>
>      // new threads should be pinned, because parent is pinned
>      printf("new threads from pinned parent sequential test (attr with
> unset cpuset):\n");
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_ZERO(&cs);
>          CPU_SET(i, &cs);
>          pthread_setaffinity_np(pthread_self(), sizeof(cs), &cs);
> @@ -209,7 +210,7 @@ int main(void)
>
>      // new threads should be pinned, because parent is pinned
>      printf("new threads from pinned parent sequential test
> (attr=NULL):\n");
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_ZERO(&cs);
>          CPU_SET(i, &cs);
>          pthread_setaffinity_np(pthread_self(), sizeof(cs), &cs);
> @@ -219,48 +220,48 @@ int main(void)
>
>
>      printf("new threads from pinned parent parallel test:\n");
> -    threads = new pthread_t[sched::cpus.size()];
> -    attrs = new pthread_attr_t[sched::cpus.size()];
> -    ref_css = new cpu_set_t[sched::cpus.size()];
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    threads = new pthread_t[get_nprocs()];
> +    attrs = new pthread_attr_t[get_nprocs()];
> +    ref_css = new cpu_set_t[get_nprocs()];
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_ZERO(&ref_css[i]);
>          CPU_SET(i, &ref_css[i]);
>          pthread_setaffinity_np(pthread_self(), sizeof(ref_css[i]),
> &ref_css[i]);
>          pthread_attr_init(&attrs[i]);
>          pthread_attr_setaffinity_np(&attrs[i], sizeof(ref_css[i]),
> &ref_css[i]);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_create(&threads[i], &attrs[i], check_cpuset, &ref_css[i]);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_join(threads[i], NULL);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_attr_destroy(&attrs[i]);
>      }
>      delete[] attrs;
>      delete[] threads;
>      delete[] ref_css;
>      // new thread on different CPU
> -    threads = new pthread_t[sched::cpus.size()];
> -    attrs = new pthread_attr_t[sched::cpus.size()];
> -    ref_css = new cpu_set_t[sched::cpus.size()];
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    threads = new pthread_t[get_nprocs()];
> +    attrs = new pthread_attr_t[get_nprocs()];
> +    ref_css = new cpu_set_t[get_nprocs()];
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_ZERO(&ref_css[i]);
> -        CPU_SET((i + 2) % sched::cpus.size(), &ref_css[i]);
> +        CPU_SET((i + 2) % get_nprocs(), &ref_css[i]);
>          pthread_attr_init(&attrs[i]);
>          pthread_attr_setaffinity_np(&attrs[i], sizeof(ref_css[i]),
> &ref_css[i]);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_ZERO(&cs);
>          CPU_SET(i, &cs);
>          pthread_setaffinity_np(pthread_self(), sizeof(cs), &cs);
>          pthread_create(&threads[i], &attrs[i], check_cpuset, &ref_css[i]);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_join(threads[i], NULL);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_attr_destroy(&attrs[i]);
>      }
>      delete[] attrs;
> @@ -268,22 +269,22 @@ int main(void)
>      delete[] ref_css;
>
>      printf("new threads from pinned parent parallel test (attr with unset
> cpuset):\n");
> -    threads = new pthread_t[sched::cpus.size()];
> -    attrs = new pthread_attr_t[sched::cpus.size()];
> -    ref_css = new cpu_set_t[sched::cpus.size()];
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    threads = new pthread_t[get_nprocs()];
> +    attrs = new pthread_attr_t[get_nprocs()];
> +    ref_css = new cpu_set_t[get_nprocs()];
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_attr_init(&attrs[i]);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_ZERO(&ref_css[i]);
>          CPU_SET(i, &ref_css[i]);
>          pthread_setaffinity_np(pthread_self(), sizeof(ref_css[i]),
> &ref_css[i]);
>          pthread_create(&threads[i], &attrs[i], check_cpuset, &ref_css[i]);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_join(threads[i], NULL);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_attr_destroy(&attrs[i]);
>      }
>      delete[] attrs;
> @@ -291,15 +292,15 @@ int main(void)
>      delete[] ref_css;
>
>      printf("new threads from pinned parent parallel test (attr=NULL):\n");
> -    threads = new pthread_t[sched::cpus.size()];
> -    ref_css = new cpu_set_t[sched::cpus.size()];
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    threads = new pthread_t[get_nprocs()];
> +    ref_css = new cpu_set_t[get_nprocs()];
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_ZERO(&ref_css[i]);
>          CPU_SET(i, &ref_css[i]);
>          pthread_setaffinity_np(pthread_self(), sizeof(ref_css[i]),
> &ref_css[i]);
>          pthread_create(&threads[i], NULL, check_cpuset, &ref_css[i]);
>      }
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_join(threads[i], NULL);
>      }
>      delete[] threads;
> diff --git a/tests/tst-pthread-affinity.cc b/tests/tst-pthread-affinity.cc
> index b62924f3..2251c741 100644
> --- a/tests/tst-pthread-affinity.cc
> +++ b/tests/tst-pthread-affinity.cc
> @@ -8,9 +8,11 @@
>  #include <stdbool.h>
>  #include <sys/time.h>
>  #include <errno.h>
> -#include <osv/sched.hh>
> +#include <sched.h>
>  #include <unistd.h>
>  #include <atomic>
> +#include <sys/sysinfo.h>
> +#include <cstring>
>
>  static std::atomic<unsigned> tests_total(0), tests_failed(0);
>
> @@ -24,13 +26,13 @@ void report(const char* name, bool passed)
>
>  void *get_processor(void *cpuid)
>  {
> -    unsigned int *c = static_cast<unsigned int *>(cpuid);
> +    int *c = static_cast<int *>(cpuid);
>
> -    report("started on the correct CPU",
> sched::thread::current()->get_cpu()->id == (*c));
> +    report("started on the correct CPU", sched_getcpu() == (*c));
>
>      usleep(1000);
>
> -    report("re-scheduled on the correct CPU",
> sched::thread::current()->get_cpu()->id == (*c));
> +    report("re-scheduled on the correct CPU", sched_getcpu() == (*c));
>
>      return NULL;
>  }
> @@ -40,13 +42,13 @@ void *get_processor(void *cpuid)
>  // current thread.
>  void *test_pin_unpin(void *)
>  {
> -    unsigned ncpus = sched::cpus.size();
> +    auto ncpus = get_nprocs();
>      // The thread starts with affinity to all cpus
>      cpu_set_t cs;
>      CPU_ZERO(&cs);
>      report("getaffinity", pthread_getaffinity_np(pthread_self(),
> sizeof(cs), &cs) == 0);
>      bool success = true;
> -    for (unsigned i = 0; i < ncpus; i++) {
> +    for (int i = 0; i < ncpus; i++) {
>          success = success && CPU_ISSET(i, &cs);
>      }
>      report("thread is initially unpinned", success);
> @@ -57,20 +59,20 @@ void *test_pin_unpin(void *)
>      CPU_ZERO(&cs);
>      report("getaffinity", pthread_getaffinity_np(pthread_self(),
> sizeof(cs), &cs) == 0);
>      success = CPU_ISSET(0, &cs);
> -    for (unsigned i = 1; i < ncpus; i++) {
> +    for (int i = 1; i < ncpus; i++) {
>          success = success && !CPU_ISSET(i, &cs);
>      }
>      report("thread is now pinned to cpu 0", success);
>      // Unpin the thread (set its affinity to all CPUs) and confirm it
> worked
>      CPU_ZERO(&cs);
> -    for (unsigned i = 0; i < ncpus; i++) {
> +    for (int i = 0; i < ncpus; i++) {
>          CPU_SET(i, &cs);
>      }
>      report("setaffinity", pthread_setaffinity_np(pthread_self(),
> sizeof(cs), &cs) == 0);
>      CPU_ZERO(&cs);
>      report("getaffinity", pthread_getaffinity_np(pthread_self(),
> sizeof(cs), &cs) == 0);
>      success = true;
> -    for (unsigned i = 0; i < ncpus; i++) {
> +    for (int i = 0; i < ncpus; i++) {
>          success = success && CPU_ISSET(i, &cs);
>      }
>      report("thread is now unpinned again", success);
> @@ -87,7 +89,7 @@ int main(void)
>      pthread_attr_t attr;
>
>      printf("sequential test:\n");
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          CPU_ZERO(&cs);
>          CPU_SET(i, &cs);
>          pthread_attr_init(&attr);
> @@ -98,11 +100,11 @@ int main(void)
>      }
>
>      printf("parallel test:\n");
> -    size_t* cpus = new size_t[sched::cpus.size()];
> -    pthread_t* threads = new pthread_t[sched::cpus.size()];
> -    pthread_attr_t* attrs = new pthread_attr_t[sched::cpus.size()];
> +    int* cpus = new int[get_nprocs()];
> +    pthread_t* threads = new pthread_t[get_nprocs()];
> +    pthread_attr_t* attrs = new pthread_attr_t[get_nprocs()];
>
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          cpus[i] = i;
>          CPU_ZERO(&cs);
>          CPU_SET(i, &cs);
> @@ -110,15 +112,15 @@ int main(void)
>          pthread_attr_setaffinity_np(&attrs[i], sizeof(cs), &cs);
>      }
>
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_create(&threads[i], &attrs[i], get_processor, &cpus[i]);
>      }
>
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_join(threads[i], NULL);
>      }
>
> -    for (size_t i = 0; i < sched::cpus.size(); i++) {
> +    for (int i = 0; i < get_nprocs(); i++) {
>          pthread_attr_destroy(&attrs[i]);
>      }
>
> diff --git a/tests/tst-yield.cc b/tests/tst-yield.cc
> index 762493f6..0baf8732 100644
> --- a/tests/tst-yield.cc
> +++ b/tests/tst-yield.cc
> @@ -5,30 +5,29 @@
>   * BSD license as described in the LICENSE file in the top-level
> directory.
>   */
>
> -#include <osv/sched.hh>
> -#include <osv/debug.hh>
> +#include <thread>
> +#include <cstdio>
>
>  int main(int argc, char **argv)
>  {
> -    debug("starting yield test\n");
> +    printf("starting yield test\n");
>
>      // Test that concurrent yield()s do not crash.
>      constexpr int N = 10;
>      constexpr int J = 10000000;
> -    sched::thread *ts[N];
> +    std::thread *ts[N];
>      for (auto &t : ts) {
> -            t = sched::thread::make([] {
> +            t = new std::thread([] {
>                  for (int j = 0; j < J; j++) {
> -                    sched::thread::yield();
> +                    std::this_thread::yield();
>                  }
>              });
> -            t->start();
>      }
>      for (auto t : ts) {
>          t->join();
>          delete t;
>      }
>
> -    debug("yield test successful\n");
> +    printf("yield test successful\n");
>      return 0;
>  }
> --
> 2.31.1
>
> --
> You received this message because you are subscribed to the Google Groups
> "OSv Development" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to osv-dev+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/osv-dev/20211119042423.55832-4-jwkozaczuk%40gmail.com
> .
>

-- 
You received this message because you are subscribed to the Google Groups "OSv 
Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to osv-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/osv-dev/CANEVyjuf7OvFVGBpKvaW7X6xeEvRBo0ucq3eDd_wyN8%3Do2Z_rw%40mail.gmail.com.

Reply via email to