I try to run v8 in os coroutine, each coroutine will use a v8::Locker, my app will create coroutine for per-request, so v8::Locker will be create and destroy frequently. thanks for your patience to explain, i will modify code to reuse coroutine and it's v8::Locker, thank you very much.
On Tuesday, December 16, 2014 4:45:12 PM UTC+8, Jakob Kummerow wrote: > > You're still creating new threads in your loop. Every one of these will > get per-thread data. > > Only one thread at a time can enter an isolate anyway -- that's the whole > point of using the locker. It's also the main reason why I don't see why > you would want to have this many threads in a real application. > - If you want to process thousands of events in the same isolate (one > after the other!), just use a fixed-size thread worker pool rather than > creating a new thread for every event. Most likely a single thread will > even be enough for this, unless the workflow consists of a small piece of > work in V8 and a lot of time spent doing or waiting for other things. > - If you want to process several things in parallel, then each thread will > need its own isolate. > > > On Tue, Dec 16, 2014 at 4:25 AM, Tom Fan <[email protected] <javascript:> > > wrote: >> >> At most one thread can run concurrently, but the performance still will >> delay. >> >> >> On Tuesday, December 16, 2014 11:18:36 AM UTC+8, Tom Fan wrote: >>> >>> I modified the test code, it will create and destory thread at loop, >>> thread do very simple thing which construct v8::locker and destruct it. I >>> found a few million cycles, the speed thread of execution slow down >>> absolutely, and the memory consumption continues to increase slowly. >>> >>> #include <pthread.h> >>> >>> #include <v8.h> >>> >>> #include <sys/time.h> >>> >>> >>> >>> double GetTime() >>> >>> { >>> >>> timeval tv; >>> >>> gettimeofday(&tv, NULL); >>> >>> return tv.tv_sec + (double)tv.tv_usec * 0.000001; >>> >>> } >>> >>> >>> >>> v8::Isolate *isolate; >>> >>> >>> >>> class Thread >>> >>> { >>> >>> public: >>> >>> static void* Start(void *thread) >>> >>> { >>> >>> static_cast<Thread*>(thread)->Start_(); >>> >>> } >>> >>> private: >>> >>> void Start_() >>> >>> { >>> >>> v8::Locker locker(isolate); >>> >>> } >>> >>> }; >>> >>> >>> >>> void test(pthread_t tid[], Thread thread[]) >>> >>> { >>> >>> for (int i = 0; i < 1; ++i) >>> >>> { >>> >>> pthread_create(&tid[i], NULL, Thread::Start, &thread[i]); >>> >>> } >>> >>> for (int i = 0; i < 1; ++i) >>> >>> { >>> >>> pthread_join(tid[i], NULL); >>> >>> } >>> >>> } >>> >>> >>> >>> int main(int argc, char* argv[]) >>> >>> { >>> >>> v8::V8::Initialize(); >>> >>> isolate = v8::Isolate::New(); >>> >>> { >>> >>> pthread_t tid[1024]; >>> >>> Thread thread[1024]; >>> >>> for (long long i = 0; i < 1000 * 1000 * 1000 * 1000llu; ++i) >>> >>> { >>> >>> double s = GetTime(); >>> >>> test(tid, thread); >>> >>> printf("%d. %f\n", i+1, GetTime() - s); >>> >>> } >>> >>> } >>> >>> isolate->Dispose(); >>> >>> v8::V8::Dispose(); >>> >>> } >>> >>> >>> >>> >>> >>> <https://lh6.googleusercontent.com/-k9kAjBeeh88/VI-i2rJ8HUI/AAAAAAAAAII/yCVL0N6tsms/s1600/b1.png> >>> >>> <https://lh3.googleusercontent.com/-cUaA-JgmfZw/VI-i8qpxvRI/AAAAAAAAAIY/CGTIkf9xZ0A/s1600/b2.png> >>> >>> >>> >>> <https://lh5.googleusercontent.com/-jYtPKqSoP-o/VI-kLqk3mNI/AAAAAAAAAI4/lG9O689fM2s/s1600/Screenshot%2Bfrom%2B2014-12-16%2B11%3A15%3A42.png> >>> >>> >>> <https://lh4.googleusercontent.com/-BL-Z1QPCNNg/VI-kXfO76LI/AAAAAAAAAJI/QkeeR2kh-OA/s1600/a2.png> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> >>> On Monday, December 15, 2014 8:36:25 PM UTC+8, Jakob Kummerow wrote: >>>> >>>> An isolate has a table with per-thread data, which is implemented as a >>>> linked list and scanned linearly. So yes, when a single isolate has seen >>>> many threads, locking it will become slower. Since there's typically no >>>> reason to access the same isolate with thousands of threads, I wouldn't >>>> consider this a bug. >>>> >>>> On Mon, Dec 15, 2014 at 11:42 AM, <[email protected]> wrote: >>>>> >>>>> >>>>> <https://lh6.googleusercontent.com/-sPNEtxGhqEc/VI6531u4HQI/AAAAAAAAAHw/2rZtAitF4bM/s1600/Screenshot%2Bfrom%2B2014-12-15%2B18%3A10%3A09.png> >>>>> My Os is fedora20, v8 version is v8-3.25.30, >>>>> first loop consumes 0.02 second, and then the 172th's loop consumes >>>>> 2.48second. >>>>> >>>>> #include <pthread.h> >>>>> #include <v8.h> >>>>> #include <sys/time.h> >>>>> >>>>> double GetTime() >>>>> { >>>>> timeval tv; >>>>> gettimeofday(&tv, NULL); >>>>> return tv.tv_sec + (double)tv.tv_usec * 0.000001; >>>>> } >>>>> >>>>> v8::Isolate *isolate; >>>>> >>>>> class Thread >>>>> { >>>>> public: >>>>> static void* Start(void *thread) >>>>> { >>>>> static_cast<Thread*>(thread)->Start_(); >>>>> } >>>>> private: >>>>> void Start_() >>>>> { >>>>> v8::Locker locker(isolate); >>>>> v8::Isolate::Scope scope(isolate); >>>>> } >>>>> >>>>> }; >>>>> >>>>> void test(pthread_t tid[], Thread thread[]) >>>>> { >>>>> for (int i = 0; i < 1024; ++i) >>>>> { >>>>> pthread_create(&tid[i], NULL, Thread::Start, &thread[i]); >>>>> } >>>>> for (int i = 0; i < 1024; ++i) >>>>> { >>>>> pthread_join(tid[i], NULL); >>>>> } >>>>> } >>>>> >>>>> int main(int argc, char* argv[]) >>>>> { >>>>> v8::V8::Initialize(); >>>>> isolate = v8::Isolate::New(); >>>>> { >>>>> pthread_t tid[1024]; >>>>> Thread thread[1024]; >>>>> for (int i = 0; i < 1000 * 1000 * 1000; ++i) >>>>> { >>>>> double s = GetTime(); >>>>> test(tid, thread); >>>>> printf("%d. %f\n", i+1, GetTime() - s); >>>>> } >>>>> } >>>>> isolate->Dispose(); >>>>> v8::V8::Dispose(); >>>>> } >>>>> >>>>> -- >>>>> -- >>>>> v8-dev mailing list >>>>> [email protected] >>>>> http://groups.google.com/group/v8-dev >>>>> --- >>>>> You received this message because you are subscribed to the Google >>>>> Groups "v8-dev" group. >>>>> To unsubscribe from this group and stop receiving emails from it, send >>>>> an email to [email protected]. >>>>> For more options, visit https://groups.google.com/d/optout. >>>>> >>>> -- >> -- >> v8-dev mailing list >> [email protected] <javascript:> >> http://groups.google.com/group/v8-dev >> --- >> You received this message because you are subscribed to the Google Groups >> "v8-dev" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > -- -- v8-dev mailing list [email protected] http://groups.google.com/group/v8-dev --- You received this message because you are subscribed to the Google Groups "v8-dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
