Skip to content

Commit 37c75e5

Browse files
committed
lib+driver+common: Move capabilities.c to v.h
It's now v_sys_get_cores under a new v_sys section. It looks a bit lonely there all by itself, but 95% of the time that function is the one I want to use.
1 parent c5d5b5a commit 37c75e5

File tree

11 files changed

+54
-80
lines changed

11 files changed

+54
-80
lines changed

include/v.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
extern "C" {
3131
#endif
3232

33+
// --- decl v_sys (System capabilities)
34+
35+
int v_sys_get_cores(void);
36+
3337
// --- decl v_mod (Runtime module loading)
3438
typedef void * v_mod_t;
3539
v_mod_t v_mod_load(const char *filename);
@@ -239,6 +243,49 @@ void *v_thread_wait(v_thread *);
239243
extern "C" {
240244
#endif
241245

246+
// --- impl v_sys (System capabilities)
247+
248+
#ifdef __APPLE__
249+
typedef unsigned int u_int;
250+
typedef unsigned char u_char;
251+
typedef unsigned short u_short;
252+
#include <sys/param.h>
253+
#include <sys/sysctl.h>
254+
#elif _WIN32
255+
#include <windows.h>
256+
#elif __linux__ || __COSMOPOLITAN__
257+
#include <unistd.h>
258+
#endif
259+
260+
int v_sys_get_cores(void) {
261+
#if defined(__APPLE__)
262+
int nm[2];
263+
size_t len = 4;
264+
uint32_t count;
265+
266+
nm[0] = CTL_HW; nm[1] = HW_AVAILCPU;
267+
sysctl(nm, 2, &count, &len, NULL, 0);
268+
269+
if (count < 1) {
270+
nm[1] = HW_NCPU;
271+
sysctl(nm, 2, &count, &len, NULL, 0);
272+
if (count < 1) {
273+
count = 1;
274+
}
275+
}
276+
return (int)count;
277+
#elif defined(_WIN32)
278+
SYSTEM_INFO sysInfo;
279+
GetSystemInfo(&sysInfo);
280+
return sysInfo.dwNumberOfProcessors;
281+
#elif defined(__linux__) || defined(__COSMOPOLITAN__)
282+
return (int)sysconf(_SC_NPROCESSORS_ONLN);
283+
#else
284+
#warning "Unknown platform, v_sys_get_cores() will return 1. Let vkoskiv know about this, please."
285+
return 1;
286+
#endif
287+
}
288+
242289
// --- impl v_mod (Runtime library loading) (c-ray) ---
243290
#if defined(WINDOWS)
244291
#include <windows.h>

src/common/json_loader.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
#include "transforms.h"
2121
#include "vector.h"
2222
#include "cr_string.h"
23-
#include "platform/capabilities.h"
2423
#include "logging.h"
2524
#include "fileio.h"
2625

@@ -79,7 +78,7 @@ void parse_prefs(struct cr_renderer *ext, const cJSON *data) {
7978
cr_renderer_set_num_pref(ext, cr_renderer_threads, threads->valueint);
8079
// prefs->fromSystem = false;
8180
} else {
82-
cr_renderer_set_num_pref(ext, cr_renderer_threads, sys_get_cores() + 2);
81+
cr_renderer_set_num_pref(ext, cr_renderer_threads, v_sys_get_cores() + 2);
8382
// prefs->fromSystem = true;
8483
}
8584
}

src/common/platform/capabilities.c

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/common/platform/capabilities.h

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/driver/args.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "args.h"
1717

1818
#include <common/platform/terminal.h>
19-
#include <common/platform/capabilities.h>
2019
#include <common/hashtable.h>
2120
#include <common/logging.h>
2221
#include <common/fileio.h>
@@ -110,9 +109,10 @@ struct driver_args *args_parse(int argc, char **argv) {
110109
if (stringEquals(argv[i], "-j")) {
111110
char *threadstr = argv[i + 1];
112111
if (threadstr) {
112+
int n_cores = v_sys_get_cores();
113113
int n = atoi(threadstr);
114114
n = n < 0 ? 0 : n;
115-
n = n > sys_get_cores() * 2 ? sys_get_cores() * 2 : n;
115+
n = n > n_cores * 2 ? n_cores * 2 : n;
116116
setDatabaseInt(args, "thread_override", n);
117117
} else {
118118
logr(warning, "Invalid -j parameter given!\n");

src/driver/main.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include <common/hashtable.h>
1818
#include <common/vendored/cJSON.h>
1919
#include <common/json_loader.h>
20-
#include <common/platform/capabilities.h>
2120
#include <encoders/encoder.h>
2221
#include <args.h>
2322
#include <sdl.h>
@@ -282,7 +281,7 @@ int main(int argc, char *argv[]) {
282281
uint64_t bounces = cr_renderer_get_num_pref(renderer, cr_renderer_bounces);
283282

284283
logr(info, "Starting c-ray renderer for frame %"PRIu64"\n", out_num);
285-
bool sys_thread = threads == (size_t)sys_get_cores() + 2;
284+
bool sys_thread = threads == (size_t)v_sys_get_cores() + 2;
286285
logr(info, "Rendering at %s%"PRIu64"%s x %s%"PRIu64"%s\n", KWHT, width, KNRM, KWHT, height, KNRM);
287286
logr(info, "Rendering %s%"PRIu64"%s samples with %s%"PRIu64"%s bounces.\n", KBLU, samples, KNRM, KGRN, bounces, KNRM);
288287
logr(info, "Rendering with %s%"PRIu64"%s%s local thread%s.\n",

src/lib/accelerators/bvh.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
#include <datatypes/poly.h>
1717
#include <renderer/instance.h>
1818
#include <common/vector.h>
19-
#include <common/platform/thread_pool.h>
20-
#include <common/platform/capabilities.h>
21-
#include <common/platform/signal.h>
2219

2320
#include <limits.h>
2421
#include <assert.h>

src/lib/api/c-ray.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <common/hashtable.h>
2525
#include <common/json_loader.h>
2626
#include <common/node_parse.h>
27-
#include <common/platform/capabilities.h>
2827
#include <common/platform/thread_pool.h>
2928
#include <common/platform/signal.h>
3029
#include <accelerators/bvh.h>
@@ -820,7 +819,7 @@ void cr_renderer_render(struct cr_renderer *ext) {
820819
r->state.clients = clients_sync(r);
821820
}
822821
if (!r->state.clients.count && !r->prefs.threads) {
823-
r->prefs.threads = sys_get_cores();
822+
r->prefs.threads = v_sys_get_cores();
824823
logr(warning, "No worker nodes and -j 0 specified, bumping threads 0 -> %zu\n", r->prefs.threads);
825824
}
826825
renderer_render(r);

src/lib/datatypes/scene.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <common/hashtable.h>
1616
#include <common/textbuffer.h>
1717
#include <common/node_parse.h>
18-
#include <common/platform/capabilities.h>
1918
#include <common/platform/thread_pool.h>
2019
#include <common/texture.h>
2120
#include "camera.h"
@@ -34,7 +33,7 @@ struct world *scene_new(void) {
3433
s->storage.node_pool = newBlock(NULL, 1024);
3534
s->storage.node_table = newHashtable(compareNodes, &s->storage.node_pool);
3635
s->bvh_lock = v_rwlock_create();
37-
s->bg_worker = thread_pool_create(sys_get_cores());
36+
s->bg_worker = thread_pool_create(v_sys_get_cores());
3837
return s;
3938
}
4039

src/lib/protocol/protocol.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
#include <common/networking.h>
2727
#include <common/base64.h>
2828
#include <common/node_parse.h>
29-
#include <common/platform/thread_pool.h>
30-
#include <common/platform/capabilities.h>
3129
#include <renderer/renderer.h>
3230
#include <renderer/instance.h>
3331
#include <datatypes/tile.h>

0 commit comments

Comments
 (0)