More worker clean up

上级 6fb6add0
...@@ -6,13 +6,15 @@ struct padded_length { ...@@ -6,13 +6,15 @@ struct padded_length {
}; };
#if defined (XV6_KERNEL) #if defined (XV6_KERNEL)
#define NWORKERS (NCPU-1) bool uwq_trywork(void);
#define NWORKERS (NCPU-1)
struct uwq; struct uwq;
struct uwq_worker { struct uwq_worker {
uwq_worker(uwq*, proc*); uwq_worker(uwq*, proc*);
long wait(); long wait();
void exit();
uwq* uwq_; uwq* uwq_;
proc *proc_; proc *proc_;
...@@ -24,14 +26,13 @@ struct uwq_worker { ...@@ -24,14 +26,13 @@ struct uwq_worker {
}; };
struct uwq : public referenced, public rcu_freed { struct uwq : public referenced, public rcu_freed {
friend struct uwq_worker;
static uwq* alloc(vmap* vmap, filetable *ftable); static uwq* alloc(vmap* vmap, filetable *ftable);
bool haswork(); bool haswork();
bool trywork(); bool tryworker();
void* buffer();
void setuentry(uptr uentry); void setuentry(uptr uentry);
// XXX(sbw) should be part of struct worker
void tryexit(uwq_worker *w);
virtual void do_gc(void) { delete this; } virtual void do_gc(void) { delete this; }
...@@ -58,6 +59,4 @@ private: ...@@ -58,6 +59,4 @@ private:
uwq_worker* worker_[NCPU]; uwq_worker* worker_[NCPU];
}; };
int uwq_trywork(void);
#endif #endif
...@@ -9,10 +9,6 @@ size_t wq_size(void); ...@@ -9,10 +9,6 @@ size_t wq_size(void);
void initwq(void); void initwq(void);
void exitwq(void); void exitwq(void);
#if defined(XV6_KERNEL)
int uwq_trywork(void);
#endif
struct work { struct work {
virtual void run() = 0; virtual void run() = 0;
}; };
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
#include "sched.hh" #include "sched.hh"
#include "percpu.hh" #include "percpu.hh"
#include "wq.hh" #include "wq.hh"
#include "uwq.hh"
#include "kmtrace.hh" #include "kmtrace.hh"
struct idle { struct idle {
...@@ -118,9 +119,7 @@ idleloop(void) ...@@ -118,9 +119,7 @@ idleloop(void)
idlem->heir = p; idlem->heir = p;
} }
// XXX(sbw) what should be the policy here? if (uwq_trywork())
worked = uwq_trywork();
if (worked == 1)
break; break;
worked = wq_trywork(); worked = wq_trywork();
......
//
// XXX
// - workers should have a uwq
// - pin workers
#include "types.h" #include "types.h"
#include "amd64.h" #include "amd64.h"
#include "kernel.hh" #include "kernel.hh"
...@@ -20,9 +15,11 @@ extern "C" { ...@@ -20,9 +15,11 @@ extern "C" {
#include "kern_c.h" #include "kern_c.h"
} }
int bool
uwq_trywork(void) uwq_trywork(void)
{ {
// Returning true means uwq added a thread to the run queue
u64 i, k; u64 i, k;
// A "random" victim CPU // A "random" victim CPU
...@@ -34,22 +31,23 @@ uwq_trywork(void) ...@@ -34,22 +31,23 @@ uwq_trywork(void)
continue; continue;
struct cpu *c = &cpus[j]; struct cpu *c = &cpus[j];
// The gc_epoch is for p and uwq
scoped_gc_epoch xgc(); scoped_gc_epoch xgc();
barrier(); barrier();
struct proc *p = c->proc; struct proc *p = c->proc;
if (p == nullptr || p->uwq == nullptr) if (p == nullptr || p->uwq == nullptr)
continue; continue;
uwq* uwq = p->uwq; uwq* uwq = p->uwq;
if (uwq->haswork()) { if (uwq->haswork()) {
if (uwq->trywork()) if (uwq->tryworker())
return 1; return true;
// XXX(sbw) start a worker thread..
break; break;
} }
} }
return 0; return false;
} }
long long
...@@ -72,14 +70,28 @@ uwq_worker::uwq_worker(uwq* u, proc* p) ...@@ -72,14 +70,28 @@ uwq_worker::uwq_worker(uwq* u, proc* p)
initcondvar(&cv_, "worker_cv"); initcondvar(&cv_, "worker_cv");
} }
void
uwq_worker::exit(void)
{
if (--uwq_->uref_ == 0)
gc_delayed(uwq_);
release(&lock_);
delete this;
::exit();
}
long long
uwq_worker::wait(void) uwq_worker::wait(void)
{ {
acquire(&lock_); acquire(&lock_);
uwq_->tryexit(this); if (uwq_->ref() == 0)
this->exit();
running_ = false; running_ = false;
cv_sleep(&cv_, &lock_); cv_sleep(&cv_, &lock_);
uwq_->tryexit(this);
if (uwq_->ref() == 0)
this->exit();
release(&lock_); release(&lock_);
return 0; return 0;
} }
...@@ -140,18 +152,6 @@ uwq::~uwq(void) ...@@ -140,18 +152,6 @@ uwq::~uwq(void)
ftable_->decref(); ftable_->decref();
} }
void
uwq::tryexit(uwq_worker *w)
{
if (ref() == 0) {
if (--uref_ == 0)
gc_delayed(this);
release(&w->lock_);
delete w;
exit();
}
}
bool bool
uwq::haswork(void) uwq::haswork(void)
{ {
...@@ -167,8 +167,9 @@ uwq::haswork(void) ...@@ -167,8 +167,9 @@ uwq::haswork(void)
} }
bool bool
uwq::trywork(void) uwq::tryworker(void)
{ {
// Try to start a worker thread
scoped_acquire lock0(&lock_); scoped_acquire lock0(&lock_);
if (ref() == 0) if (ref() == 0)
...@@ -249,12 +250,6 @@ uwq::onzero() const ...@@ -249,12 +250,6 @@ uwq::onzero() const
u->finish(); u->finish();
} }
void*
uwq::buffer(void)
{
return (void*)len_;
}
void void
uwq::setuentry(uptr uentry) uwq::setuentry(uptr uentry)
{ {
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论