blob: 59ed369067f64231e4919299b14a21a627aef354 [file] [log] [blame]
Vijay Pai0e66efd2016-01-13 11:15:49 -08001/*
2 *
vjpai861eb9f2016-01-19 13:37:27 -08003 * Copyright 2016, Google Inc.
Vijay Pai0e66efd2016-01-13 11:15:49 -08004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
vjpai4f5146d2016-01-19 15:28:52 -080034#include "test/cpp/qps/limit_cores.h"
Vijay Pai0e66efd2016-01-13 11:15:49 -080035
Vijay Pai0e66efd2016-01-13 11:15:49 -080036#include <grpc/support/cpu.h>
37#include <grpc/support/log.h>
38#include <grpc/support/port_platform.h>
39
Vijay Pai0e66efd2016-01-13 11:15:49 -080040#ifdef GPR_CPU_LINUX
41#ifndef _GNU_SOURCE
42#define _GNU_SOURCE
43#endif
44#include <sched.h>
David Garcia Quintas711766d2016-03-21 11:27:37 -070045
46namespace grpc {
47namespace testing {
48
vjpai574020e2016-02-03 07:58:23 -080049int LimitCores(const int* cores, int cores_size) {
Vijay Pai33e51182016-02-01 16:40:06 -080050 const int num_cores = gpr_cpu_num_cores();
Vijay Paic64736d2016-02-01 09:33:11 -080051 int cores_set = 0;
Vijay Pai0e66efd2016-01-13 11:15:49 -080052
vjpai574020e2016-02-03 07:58:23 -080053 cpu_set_t* cpup = CPU_ALLOC(num_cores);
Vijay Paic64736d2016-02-01 09:33:11 -080054 GPR_ASSERT(cpup);
Vijay Pai33e51182016-02-01 16:40:06 -080055 const size_t size = CPU_ALLOC_SIZE(num_cores);
Vijay Paic64736d2016-02-01 09:33:11 -080056 CPU_ZERO_S(size, cpup);
57
58 if (cores_size > 0) {
59 for (int i = 0; i < cores_size; i++) {
60 if (cores[i] < num_cores) {
61 CPU_SET_S(cores[i], size, cpup);
62 cores_set++;
63 }
Vijay Pai0e66efd2016-01-13 11:15:49 -080064 }
Vijay Pai0e66efd2016-01-13 11:15:49 -080065 } else {
Vijay Paic64736d2016-02-01 09:33:11 -080066 for (int i = 0; i < num_cores; i++) {
67 CPU_SET_S(i, size, cpup);
68 cores_set++;
69 }
Vijay Pai0e66efd2016-01-13 11:15:49 -080070 }
Vijay Paic64736d2016-02-01 09:33:11 -080071 GPR_ASSERT(sched_setaffinity(0, size, cpup) == 0);
72 CPU_FREE(cpup);
73 return cores_set;
Vijay Pai0e66efd2016-01-13 11:15:49 -080074}
David Garcia Quintas711766d2016-03-21 11:27:37 -070075
David Garcia Quintasca2886a2016-03-21 12:04:18 -070076} // namespace testing
77} // namespace grpc
78#else
David Garcia Quintas711766d2016-03-21 11:27:37 -070079namespace grpc {
80namespace testing {
81
Vijay Pai0e66efd2016-01-13 11:15:49 -080082// LimitCores is not currently supported for non-Linux platforms
vjpai574020e2016-02-03 07:58:23 -080083int LimitCores(const int*, int) { return gpr_cpu_num_cores(); }
David Garcia Quintasca2886a2016-03-21 12:04:18 -070084
Vijay Pai0e66efd2016-01-13 11:15:49 -080085} // namespace testing
86} // namespace grpc
David Garcia Quintasca2886a2016-03-21 12:04:18 -070087#endif