More usched tweaks

上级 f6d240e6
NCXXFLAGS = -static -g -MD -m64 -O3 -Wall -Werror -DHW_$(HW) \ NCXXFLAGS = -g -static -MD -m64 -O3 -Wall -Werror -DHW_$(HW) \
-fno-builtin -fno-strict-aliasing -fno-omit-frame-pointer \ -fno-builtin -fno-strict-aliasing -fno-omit-frame-pointer \
-fms-extensions -mcx16 -mno-red-zone -std=c++0x \ -fms-extensions -mcx16 -mno-red-zone -std=c++0x \
-Wno-sign-compare -fno-exceptions -fno-rtti -fcheck-new \ -Wno-sign-compare -fno-exceptions -fno-rtti -fcheck-new \
-iquote . -iquote include \ -iquote . -iquote include \
-include param.h -include include/compiler.h -include param.h -include include/compiler.h
NCFLAGS = -static -g -MD -m64 -O3 -Wall -Werror -DHW_$(HW) \ NCFLAGS = -g -static -MD -m64 -O3 -Wall -Werror -DHW_$(HW) \
-fno-builtin -fno-strict-aliasing -fno-omit-frame-pointer \ -fno-builtin -fno-strict-aliasing -fno-omit-frame-pointer \
-fms-extensions -mcx16 -mno-red-zone -std=c99 \ -fms-extensions -mcx16 -mno-red-zone -std=c99 \
-Wno-sign-compare -fno-exceptions \ -Wno-sign-compare -fno-exceptions \
...@@ -25,7 +25,7 @@ $(O)/user/%.o: user/%.cc ...@@ -25,7 +25,7 @@ $(O)/user/%.o: user/%.cc
$(O)/%: $(O)/user/%.o $(O)/%: $(O)/user/%.o
@echo " LD $@" @echo " LD $@"
$(Q)mkdir -p $(@D) $(Q)mkdir -p $(@D)
$(Q)$(CXX) -o $@ $^ -lpthread $(Q)$(CXX) -o $@ $^ -lpthread -ljemalloc
.PRECIOUS: $(O)/user/%.o .PRECIOUS: $(O)/user/%.o
-include $(O)/user/*.d -include $(O)/user/*.d
......
// export LD_PRELOAD="/usr/lib/libjemalloc.so.1"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <stdint.h>
...@@ -13,11 +15,13 @@ typedef uint64_t u64; ...@@ -13,11 +15,13 @@ typedef uint64_t u64;
#include "sched.hh" #include "sched.hh"
#include "queue.h" #include "queue.h"
static int nprocs = 2; static int nprocs = NCPU;
static int the_time = 2; static int the_time = 5;
static uint64_t start; static uint64_t start;
static volatile int go; static volatile int go;
static __thread unsigned myid_;
percpu<uint64_t> ops; percpu<uint64_t> ops;
struct proc : public sched_link struct proc : public sched_link
...@@ -26,8 +30,6 @@ struct proc : public sched_link ...@@ -26,8 +30,6 @@ struct proc : public sched_link
LIST_ENTRY(proc) link; LIST_ENTRY(proc) link;
}; };
//percpu<proc_list> proc_list;
struct stuff { struct stuff {
bool flipper; bool flipper;
uint32_t seed; uint32_t seed;
...@@ -38,24 +40,17 @@ struct stuff { ...@@ -38,24 +40,17 @@ struct stuff {
} }
proc* allocproc() { proc* allocproc() {
if (LIST_EMPTY(&proc_list)) proc* p = (proc*) malloc(sizeof(proc));
die("allocproc");
proc* p = LIST_FIRST(&proc_list);
LIST_REMOVE(p, link);
return p; return p;
} }
void freeproc(proc* p) { void freeproc(proc* p) {
LIST_INSERT_HEAD(&proc_list, p, link); free(p);
} }
void void
fillproc(void) fillproc(void)
{ {
for (int i = 0; i < 20; i++) {
proc* p = new proc();
freeproc(p);
}
} }
}; };
percpu<stuff> stuff_; percpu<stuff> stuff_;
...@@ -71,14 +66,14 @@ public: ...@@ -71,14 +66,14 @@ public:
volatile u64 ncansteal_ __mpalign__; volatile u64 ncansteal_ __mpalign__;
sched_stat stats_;
private: private:
pthread_spinlock_t lock_; pthread_spinlock_t lock_;
sched_link head_; sched_link head_;
}; };
percpu<schedule> schedule_; percpu<schedule> schedule_;
static __thread unsigned myid_;
int int
mycpuid(void) mycpuid(void)
{ {
...@@ -103,7 +98,7 @@ sighandler(int x) ...@@ -103,7 +98,7 @@ sighandler(int x)
tot = 0; tot = 0;
for (int i = 0; i < NCPU; i++) { for (int i = 0; i < NCPU; i++) {
tot += ops[i]; tot += ops[i];
printf(" %lu\n", schedule_[i].ncansteal_); printf(" %lu %lu\n", ops[i], schedule_[i].stats_.steals);
} }
printf("%f\n", (stop-start)/(tot/NCPU)); printf("%f\n", (stop-start)/(tot/NCPU));
...@@ -129,6 +124,7 @@ schedule::enq(proc* p) ...@@ -129,6 +124,7 @@ schedule::enq(proc* p)
head_.prev = entry; head_.prev = entry;
if (cansteal((proc*)entry, true)) if (cansteal((proc*)entry, true))
ncansteal_++; ncansteal_++;
stats_.enqs++;
pthread_spin_unlock(&lock_); pthread_spin_unlock(&lock_);
} }
...@@ -150,6 +146,7 @@ schedule::deq(void) ...@@ -150,6 +146,7 @@ schedule::deq(void)
entry->prev->next = entry->next; entry->prev->next = entry->next;
if (cansteal((proc*)entry, true)) if (cansteal((proc*)entry, true))
--ncansteal_; --ncansteal_;
stats_.deqs++;
pthread_spin_unlock(&lock_); pthread_spin_unlock(&lock_);
return (proc*)entry; return (proc*)entry;
} }
...@@ -165,10 +162,12 @@ schedule::steal(bool nonexec) ...@@ -165,10 +162,12 @@ schedule::steal(bool nonexec)
ptr->next->prev = ptr->prev; ptr->next->prev = ptr->prev;
ptr->prev->next = ptr->next; ptr->prev->next = ptr->next;
--ncansteal_; --ncansteal_;
++stats_.steals;
pthread_spin_unlock(&lock_); pthread_spin_unlock(&lock_);
return (proc*)ptr; return (proc*)ptr;
} }
pthread_spin_unlock(&lock_); pthread_spin_unlock(&lock_);
++stats_.misses;
return nullptr; return nullptr;
} }
...@@ -190,7 +189,7 @@ stealit(void) ...@@ -190,7 +189,7 @@ stealit(void)
for (int i = 0; i < NCPU; i++) { for (int i = 0; i < NCPU; i++) {
if (i == myid_) if (i == myid_)
break; continue;
p = schedule_[i].steal(true); p = schedule_[i].steal(true);
if (p) { if (p) {
return p; return p;
...@@ -214,16 +213,14 @@ schedit(void) ...@@ -214,16 +213,14 @@ schedit(void)
runit(p); runit(p);
(*ops)++; (*ops)++;
if (r < 10 && !stuff_->flipper) { if (r < 10 && (myid_%2) == 0) {
stuff_->flipper = !stuff_->flipper;
stuff_->freeproc(p); stuff_->freeproc(p);
} }
else else
schedule_->enq(p); schedule_->enq(p);
} }
if (r >= 10 && r < 20 && stuff_->flipper) { if (r > 90 && (myid_%2) == 1) {
stuff_->flipper = !stuff_->flipper;
schedule_->enq(stuff_->allocproc()); schedule_->enq(stuff_->allocproc());
} }
} }
...@@ -238,7 +235,7 @@ worker(void* x) ...@@ -238,7 +235,7 @@ worker(void* x)
if (myid_ == 0) { if (myid_ == 0) {
for (int i = 0; i < nprocs; i++) for (int i = 0; i < nprocs; i++)
schedule_->enq(new proc()); schedule_->enq(stuff_->allocproc());
if (signal(SIGALRM, sighandler) == SIG_ERR) if (signal(SIGALRM, sighandler) == SIG_ERR)
edie("signal failed\n"); edie("signal failed\n");
...@@ -270,6 +267,7 @@ main(int ac, char** av) ...@@ -270,6 +267,7 @@ main(int ac, char** av)
edie("pthread_create"); edie("pthread_create");
} }
sleep(1);
worker((void*)0); worker((void*)0);
return 0; return 0;
} }
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论