Skip to content

Commit f724043

Browse files
committed
---
yaml --- r: 4231 b: refs/heads/master c: 4ef1ec5 h: refs/heads/master i: 4229: 7921b9b 4227: 980b3f1 4223: 5bdbd02 v: v3
1 parent 057d231 commit f724043

18 files changed

+183
-87
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: 75985ab75ed216cd8c873c9ef08cd88708f8354f
2+
refs/heads/master: 4ef1ec580aaf9f95d66c1654ce942f5e454a0b4d

trunk/mk/rt.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ RUNTIME_CS := rt/sync/timer.cpp \
1010
rt/rust_run_program.cpp \
1111
rt/rust_crate_cache.cpp \
1212
rt/rust_comm.cpp \
13+
rt/rust_env.cpp \
1314
rt/rust_scheduler.cpp \
1415
rt/rust_task.cpp \
1516
rt/rust_task_list.cpp \
@@ -38,6 +39,7 @@ RUNTIME_HDR := rt/globals.h \
3839
rt/rust_internal.h \
3940
rt/rust_util.h \
4041
rt/rust_chan.h \
42+
rt/rust_env.h \
4143
rt/rust_port.h \
4244
rt/rust_scheduler.h \
4345
rt/rust_task.h \

trunk/src/rt/memory_region.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ memory_region::alloc_header *memory_region::get_header(void *mem) {
1414

1515
memory_region::memory_region(rust_srv *srv, bool synchronized) :
1616
_srv(srv), _parent(NULL), _live_allocations(0),
17-
_detailed_leaks(getenv("RUST_DETAILED_LEAKS") != NULL),
17+
_detailed_leaks(srv->env->detailed_leaks),
1818
_synchronized(synchronized), _hack_allow_leaks(false) {
1919
}
2020

trunk/src/rt/rust.cpp

Lines changed: 8 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -76,74 +76,23 @@ command_line_args : public kernel_owned<command_line_args>
7676
};
7777

7878

79-
#if defined(__WIN32__)
80-
int get_num_cpus() {
81-
SYSTEM_INFO sysinfo;
82-
GetSystemInfo(&sysinfo);
83-
84-
return (int) sysinfo.dwNumberOfProcessors;
85-
}
86-
#elif defined(__BSD__)
87-
int get_num_cpus() {
88-
/* swiped from http://stackoverflow.com/questions/150355/
89-
programmatically-find-the-number-of-cores-on-a-machine */
90-
91-
unsigned int numCPU;
92-
int mib[4];
93-
size_t len = sizeof(numCPU);
94-
95-
/* set the mib for hw.ncpu */
96-
mib[0] = CTL_HW;
97-
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
98-
99-
/* get the number of CPUs from the system */
100-
sysctl(mib, 2, &numCPU, &len, NULL, 0);
101-
102-
if( numCPU < 1 ) {
103-
mib[1] = HW_NCPU;
104-
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
105-
106-
if( numCPU < 1 ) {
107-
numCPU = 1;
108-
}
109-
}
110-
return numCPU;
111-
}
112-
#elif defined(__GNUC__)
113-
int get_num_cpus() {
114-
return sysconf(_SC_NPROCESSORS_ONLN);
115-
}
116-
#endif
117-
118-
int get_num_threads()
119-
{
120-
char *env = getenv("RUST_THREADS");
121-
if(env) {
122-
int num = atoi(env);
123-
if(num > 0)
124-
return num;
125-
}
126-
return get_num_cpus();
127-
}
128-
12979
/**
13080
* Main entry point into the Rust runtime. Here we create a Rust service,
13181
* initialize the kernel, create the root domain and run it.
13282
*/
13383

13484
int check_claims = 0;
13585

136-
void enable_claims(void* ck) { check_claims = (ck != 0); }
137-
13886
extern "C" CDECL int
13987
rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) {
14088

141-
update_log_settings(crate_map, getenv("RUST_LOG"));
142-
enable_claims(getenv("CHECK_CLAIMS"));
143-
int num_threads = get_num_threads();
89+
rust_env *env = load_env();
90+
91+
update_log_settings(crate_map, env->logspec);
92+
check_claims = env->check_claims;
14493

145-
rust_srv *srv = new rust_srv();
146-
rust_kernel *kernel = new rust_kernel(srv, num_threads);
94+
rust_srv *srv = new rust_srv(env);
95+
rust_kernel *kernel = new rust_kernel(srv, env->num_sched_threads);
14796
kernel->start();
14897
rust_task *root_task = kernel->create_task(NULL, "main");
14998
rust_scheduler *sched = root_task->sched;
@@ -159,13 +108,13 @@ rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) {
159108

160109
root_task->start(main_fn, (uintptr_t)args->args);
161110

162-
DLOG(sched, dom, "Using %d worker threads.", num_threads);
163-
164111
int ret = kernel->start_task_threads();
165112
delete args;
166113
delete kernel;
167114
delete srv;
168115

116+
free_env(env);
117+
169118
#if !defined(__WIN32__)
170119
// Don't take down the process if the main thread exits without an
171120
// error.

trunk/src/rt/rust_builtin.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -857,10 +857,10 @@ clone_chan(rust_task *task, rust_chan *chan) {
857857
}
858858

859859
// defined in rust_task.cpp
860-
extern size_t g_min_stack_size;
860+
extern size_t g_custom_min_stack_size;
861861
extern "C" CDECL void
862862
set_min_stack(rust_task *task, uintptr_t stack_size) {
863-
g_min_stack_size = stack_size;
863+
g_custom_min_stack_size = stack_size;
864864
}
865865

866866
//

trunk/src/rt/rust_env.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// The runtime wants to pull a number of variables out of the
2+
// environment but calling getenv is not threadsafe, so every value
3+
// that might come from the environment is loaded here, once, during
4+
// init.
5+
6+
#include "rust_internal.h"
7+
8+
// The environment variables that the runtime knows about
9+
#define RUST_THREADS "RUST_THREADS"
10+
#define RUST_MIN_STACK "RUST_MIN_STACK"
11+
#define RUST_LOG "RUST_LOG"
12+
#define CHECK_CLAIMS "CHECK_CLAIMS"
13+
#define DETAILED_LEAKS "DETAILED_LEAKS"
14+
#define RUST_SEED "RUST_SEED"
15+
16+
#if defined(__WIN32__)
17+
static int
18+
get_num_cpus() {
19+
SYSTEM_INFO sysinfo;
20+
GetSystemInfo(&sysinfo);
21+
22+
return (int) sysinfo.dwNumberOfProcessors;
23+
}
24+
#elif defined(__BSD__)
25+
static int
26+
get_num_cpus() {
27+
/* swiped from http://stackoverflow.com/questions/150355/
28+
programmatically-find-the-number-of-cores-on-a-machine */
29+
30+
unsigned int numCPU;
31+
int mib[4];
32+
size_t len = sizeof(numCPU);
33+
34+
/* set the mib for hw.ncpu */
35+
mib[0] = CTL_HW;
36+
mib[1] = HW_AVAILCPU; // alternatively, try HW_NCPU;
37+
38+
/* get the number of CPUs from the system */
39+
sysctl(mib, 2, &numCPU, &len, NULL, 0);
40+
41+
if( numCPU < 1 ) {
42+
mib[1] = HW_NCPU;
43+
sysctl( mib, 2, &numCPU, &len, NULL, 0 );
44+
45+
if( numCPU < 1 ) {
46+
numCPU = 1;
47+
}
48+
}
49+
return numCPU;
50+
}
51+
#elif defined(__GNUC__)
52+
static int
53+
get_num_cpus() {
54+
return sysconf(_SC_NPROCESSORS_ONLN);
55+
}
56+
#endif
57+
58+
static int
59+
get_num_threads()
60+
{
61+
char *env = getenv(RUST_THREADS);
62+
if(env) {
63+
int num = atoi(env);
64+
if(num > 0)
65+
return num;
66+
}
67+
return get_num_cpus();
68+
}
69+
70+
// FIXME (issue #151): This should be 0x300; the change here is for
71+
// practicality's sake until stack growth is working.
72+
73+
static size_t
74+
get_min_stk_size() {
75+
char *stack_size = getenv(RUST_MIN_STACK);
76+
if(stack_size) {
77+
return strtol(stack_size, NULL, 0);
78+
}
79+
else {
80+
return 0x300000;
81+
}
82+
}
83+
84+
static char*
85+
copyenv(const char* name) {
86+
char *envvar = getenv(name);
87+
if (!envvar) {
88+
return NULL;
89+
} else {
90+
const size_t maxlen = 4096;
91+
size_t strlen = strnlen(envvar, maxlen);
92+
size_t buflen = strlen + 1;
93+
char *var = (char*)malloc(buflen);
94+
memset(var, 0, buflen);
95+
strncpy(var, envvar, strlen);
96+
return var;
97+
}
98+
}
99+
100+
rust_env*
101+
load_env() {
102+
rust_env *env = (rust_env*)malloc(sizeof(rust_env));
103+
104+
env->num_sched_threads = (size_t)get_num_threads();
105+
env->min_stack_size = get_min_stk_size();
106+
env->logspec = copyenv(RUST_LOG);
107+
env->check_claims = getenv(CHECK_CLAIMS) != NULL;
108+
env->detailed_leaks = getenv(DETAILED_LEAKS) != NULL;
109+
env->rust_seed = copyenv(RUST_SEED);
110+
111+
return env;
112+
}
113+
114+
void
115+
free_env(rust_env *env) {
116+
free(env->logspec);
117+
free(env->rust_seed);
118+
free(env);
119+
}

trunk/src/rt/rust_env.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct rust_env {
2+
size_t num_sched_threads;
3+
size_t min_stack_size;
4+
char* logspec;
5+
bool check_claims;
6+
bool detailed_leaks;
7+
char* rust_seed;
8+
};
9+
10+
rust_env* load_env();
11+
void free_env(rust_env *rust_env);

trunk/src/rt/rust_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "rust.h"
2525
#include "rand.h"
2626
#include "uthash.h"
27+
#include "rust_env.h"
2728

2829
#if defined(__WIN32__)
2930
extern "C" {

trunk/src/rt/rust_kernel.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ rust_kernel::rust_kernel(rust_srv *srv, size_t num_threads) :
1212
_interrupt_kernel_loop(FALSE),
1313
num_threads(num_threads),
1414
rval(0),
15-
live_tasks(0)
15+
live_tasks(0),
16+
env(srv->env)
1617
{
1718
isaac_init(this, &rctx);
1819
create_schedulers();
@@ -51,6 +52,8 @@ rust_kernel::destroy_scheduler(rust_scheduler *sched) {
5152
}
5253

5354
void rust_kernel::create_schedulers() {
55+
KLOG_("Using %d scheduler threads.", num_threads);
56+
5457
for(size_t i = 0; i < num_threads; ++i) {
5558
threads.push(create_scheduler(i));
5659
}

trunk/src/rt/rust_kernel.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class rust_kernel : public rust_thread {
9898
*/
9999
indexed_list<rust_message_queue> message_queues;
100100

101+
struct rust_env *env;
102+
101103
rust_handle<rust_task> *get_task_handle(rust_task *task);
102104
rust_handle<rust_port> *get_port_handle(rust_port *port);
103105

trunk/src/rt/rust_scheduler.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ rust_scheduler::rust_scheduler(rust_kernel *kernel,
2020
cache(this),
2121
kernel(kernel),
2222
message_queue(message_queue),
23-
id(id)
23+
id(id),
24+
min_stack_size(kernel->env->min_stack_size),
25+
env(kernel->env)
2426
{
2527
LOGPTR(this, "new dom", (uintptr_t)this);
2628
isaac_init(this, &rctx);

trunk/src/rt/rust_scheduler.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ struct rust_scheduler : public kernel_owned<rust_scheduler>,
6060
const int id;
6161

6262
lock_and_signal lock;
63+
size_t min_stack_size;
6364

6465
#ifndef __WIN32__
6566
pthread_attr_t attr;
6667
#endif
6768

69+
rust_env *env;
70+
6871
// Only a pointer to 'name' is kept, so it must live as long as this
6972
// domain.
7073
rust_scheduler(rust_kernel *kernel,

trunk/src/rt/rust_srv.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "rust_internal.h"
22
#include "rust_srv.h"
33

4-
rust_srv::rust_srv() :
4+
rust_srv::rust_srv(rust_env *env) :
5+
env(env),
56
local_region(this, false) {
67
}
78

@@ -72,7 +73,7 @@ rust_srv::warning(char const *expression,
7273

7374
rust_srv *
7475
rust_srv::clone() {
75-
return new rust_srv();
76+
return new rust_srv(env);
7677
}
7778

7879
//

trunk/src/rt/rust_srv.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
class rust_srv {
88
public:
9+
rust_env *env;
910
memory_region local_region;
1011
virtual void log(char const *msg);
1112
virtual void fatal(char const *expression,
@@ -21,7 +22,7 @@ class rust_srv {
2122
virtual void free(void *);
2223
virtual void *malloc(size_t);
2324
virtual void *realloc(void *, size_t);
24-
rust_srv();
25+
rust_srv(rust_env *);
2526
virtual ~rust_srv();
2627
virtual rust_srv *clone();
2728
};

0 commit comments

Comments
 (0)