]> gitweb.factorcode.org Git - factor.git/commitdiff
vm: count samples during unoptimized compiler
authorJoe Groff <arcata@gmail.com>
Wed, 2 Nov 2011 06:40:46 +0000 (23:40 -0700)
committerJoe Groff <arcata@gmail.com>
Wed, 2 Nov 2011 20:23:21 +0000 (13:23 -0700)
basis/tools/profiler/sampling/sampling.factor
vm/jit.cpp
vm/jit.hpp
vm/sampling_profiler.cpp
vm/sampling_profiler.hpp
vm/vm.cpp
vm/vm.hpp

index 92374bb1c32b2ba950965fc9798de335cc4a441b..bb95a6270665b4a4ecc2ebc88cb6a1fe12e06c4c 100644 (file)
@@ -1,9 +1,10 @@
 ! (c)2011 Joe Groff bsd license
 USING: accessors assocs calendar combinators
-combinators.short-circuit continuations fry io kernel
-kernel.private locals math math.statistics math.vectors memory
-namespaces prettyprint sequences sets sorting
-tools.profiler.sampling.private hashtables.identity generalizations ;
+combinators.short-circuit continuations fry generalizations
+hashtables.identity io kernel kernel.private locals math
+math.statistics math.vectors memory namespaces prettyprint
+sequences sequences.generalizations sets sorting
+tools.profiler.sampling.private ;
 FROM: sequences => change-nth ;
 FROM: assocs => change-at ;
 IN: tools.profiler.sampling
@@ -26,12 +27,17 @@ CONSTANT: ignore-words
     [ 0 profiling ] [ ] cleanup
     (get-samples) raw-profile-data set-global ; inline
 
-: total-sample-count ( sample -- count ) first ;
-: gc-sample-count ( sample -- count ) second ;
-: foreign-sample-count ( sample -- count ) third ;
-: foreign-thread-sample-count ( sample -- count ) fourth ;
-: sample-thread ( sample -- alien ) 4 swap nth ;
-: sample-callstack ( sample -- array ) 5 swap nth ;
+: total-sample-count ( sample -- count ) 0 swap nth ;
+: gc-sample-count ( sample -- count ) 1 swap nth ;
+: jit-sample-count ( sample -- count ) 2 swap nth ;
+: foreign-sample-count ( sample -- count ) 3 swap nth ;
+: foreign-thread-sample-count ( sample -- count ) 4 swap nth ;
+: sample-counts-slice ( sample -- counts ) 5 head-slice ;
+
+: sample-thread ( sample -- alien ) 5 swap nth ;
+: sample-callstack ( sample -- array ) 6 swap nth ;
+: unclip-callstack ( sample -- sample' callstack-top )
+    clone 6 over [ unclip swap ] change-nth ;
 
 : samples>time ( samples -- time )
     samples-per-second get-global / seconds ;
@@ -63,23 +69,22 @@ CONSTANT: ignore-words
 : time-per-thread ( -- n )
     get-raw-profile-data collect-threads [ (total-time) ] assoc-map ;
 
-: unclip-callstack ( sample -- sample' callstack-top )
-    clone 5 over [ unclip swap ] change-nth ;
-
 : leaf-callstack? ( callstack -- ? )
     [ ignore-word? ] all? ;
 
-: sum-times ( samples -- times )
-    { 0 0 0 0 } [ 4 head-slice v+ ] reduce ;
+CONSTANT: zero-counts { 0 0 0 0 0 }
+
+: sum-counts ( samples -- times )
+    zero-counts [ sample-counts-slice v+ ] reduce ;
 
 TUPLE: profile-node
-    total-time gc-time foreign-time foreign-thread-time children ;
+    total-time gc-time jit-time foreign-time foreign-thread-time children ;
 
 : <profile-node> ( times children -- node )
-    [ first4 [ samples>time ] 4 napply ] dip profile-node boa ;
+    [ 5 firstn [ samples>time ] 5 napply ] dip profile-node boa ;
 
 : <profile-root-node> ( samples collector-quot -- node )
-    [ sum-times ] swap bi <profile-node> ; inline
+    [ sum-counts ] swap bi <profile-node> ; inline
 
 :: (collect-subtrees) ( samples child-quot -- children )
     samples [ sample-callstack leaf-callstack? not ] filter
@@ -87,7 +92,7 @@ TUPLE: profile-node
 
 : collect-tops ( samples -- node )
     [ unclip-callstack ] collect-pairs [
-        [ sum-times ]
+        [ sum-counts ]
         [ [ collect-tops ] (collect-subtrees) ] bi <profile-node>
     ] assoc-map ;
 
@@ -112,7 +117,7 @@ TUPLE: profile-node
     IH{ } clone :> per-word-samples
     samples [| sample |
         sample sample-callstack unique keys [ ignore-word? not ] filter [
-            per-word-samples [ { 0 0 0 0 } or sample 4 head-slice v+ ] change-at
+            per-word-samples [ zero-counts or sample sample-counts-slice v+ ] change-at
         ] each
     ] each
     per-word-samples [ f <profile-node> ] assoc-map ;
@@ -137,16 +142,21 @@ TUPLE: profile-node
     >alist [ second total-time>> ] inv-sort-with ;
 
 : duration. ( duration -- )
-    duration>milliseconds >integer pprint "ms" write ;
+    samples-per-second get-global {
+        { [ dup 1000 <= ] [ drop duration>milliseconds >integer pprint "ms" write ] }
+        { [ dup 1,000,000 <= ] [ drop duration>microseconds >integer pprint "µs" write ] }
+        [ drop duration>nanoseconds >integer pprint "ns" write ]
+    } cond ;
 
 DEFER: (profile.)
 
 : times. ( node -- )
     {
-        [ total-time>> duration. " (" write ]
-        [ gc-time>> duration. " gc, " write ]
-        [ foreign-time>> duration. " foreign, " write ]
-        [ foreign-thread-time>> duration. " foreign threads)" write ]
+        [ total-time>> duration. ]
+        [ " (GC:" write gc-time>> duration. ]
+        [ ", JIT:" write jit-time>> duration. ]
+        [ ", FFI:" write foreign-time>> duration. ]
+        [ ", FT:" write foreign-thread-time>> duration. ")" write ]
     } cleave ;
 
 :: (profile-node.) ( word node depth -- )
index 77b827bef2c290a3ceb8c3b64072f515adc744f2..25ae68bd8d93c42b3b122ea7fca3e4a322b529a7 100644 (file)
@@ -21,7 +21,16 @@ jit::jit(code_block_type type_, cell owner_, factor_vm *vm)
          position(0),
          offset(0),
          parent(vm)
-{}
+{
+       fixnum count = atomic::add(&parent->current_jit_count, 1);
+       assert(count >= 1);
+}
+
+jit::~jit()
+{
+       fixnum count = atomic::subtract(&parent->current_jit_count, 1);
+       assert(count >= 0);
+}
 
 void jit::emit_relocation(cell relocation_template_)
 {
index 4649579a47b0f9c0aab3d93941ef430a8ccbce4a..1024751747f2a068930eb64b3d65e8423d204d3f 100644 (file)
@@ -14,6 +14,8 @@ struct jit {
        factor_vm *parent;
 
        explicit jit(code_block_type type, cell owner, factor_vm *parent);
+       ~jit();
+
        void compute_position(cell offset);
 
        void emit_relocation(cell relocation_template);
@@ -67,6 +69,10 @@ struct jit {
 
        
        code_block *to_code_block();
+
+private:
+       jit(const jit&);
+       void operator=(const jit&);
 };
 
 }
index d3c20111e0592173b45c911b00f941201b39133b..1f87209f6ade4fefdd4d16bd9a72fae3826060bd 100644 (file)
@@ -9,10 +9,12 @@ profiling_sample_count profiling_sample_count::record_counts() volatile
        profiling_sample_count returned(
                sample_count,
                gc_sample_count,
+               jit_sample_count,
                foreign_sample_count,
                foreign_thread_sample_count);
        atomic::subtract(&sample_count, returned.sample_count);
        atomic::subtract(&gc_sample_count, returned.gc_sample_count);
+       atomic::subtract(&jit_sample_count, returned.jit_sample_count);
        atomic::subtract(&foreign_sample_count, returned.foreign_sample_count);
        atomic::subtract(&foreign_thread_sample_count, returned.foreign_thread_sample_count);
        return returned;
@@ -22,6 +24,7 @@ void profiling_sample_count::clear() volatile
 {
        sample_count = 0;
        gc_sample_count = 0;
+       jit_sample_count = 0;
        foreign_sample_count = 0;
        foreign_thread_sample_count = 0;
        atomic::fence();
@@ -114,14 +117,15 @@ void factor_vm::primitive_get_samples()
 
                for (; from_iter != samples.end(); ++from_iter, ++to_i)
                {
-                       data_root<array> sample(allot_array(6, false_object),this);
+                       data_root<array> sample(allot_array(7, false_object),this);
 
                        set_array_nth(sample.untagged(),0,tag_fixnum(from_iter->counts.sample_count));
                        set_array_nth(sample.untagged(),1,tag_fixnum(from_iter->counts.gc_sample_count));
-                       set_array_nth(sample.untagged(),2,tag_fixnum(from_iter->counts.foreign_sample_count));
-                       set_array_nth(sample.untagged(),3,tag_fixnum(from_iter->counts.foreign_thread_sample_count));
+                       set_array_nth(sample.untagged(),2,tag_fixnum(from_iter->counts.jit_sample_count));
+                       set_array_nth(sample.untagged(),3,tag_fixnum(from_iter->counts.foreign_sample_count));
+                       set_array_nth(sample.untagged(),4,tag_fixnum(from_iter->counts.foreign_thread_sample_count));
 
-                       set_array_nth(sample.untagged(),4,from_iter->thread);
+                       set_array_nth(sample.untagged(),5,from_iter->thread);
 
                        cell callstack_size = from_iter->callstack_end - from_iter->callstack_begin;
                        data_root<array> callstack(allot_array(callstack_size,false_object),this);
@@ -135,7 +139,7 @@ void factor_vm::primitive_get_samples()
                        for (; c_from_iter != c_from_iter_end; ++c_from_iter, ++c_to_i)
                                set_array_nth(callstack.untagged(),c_to_i,*c_from_iter);
 
-                       set_array_nth(sample.untagged(),5,callstack.value());
+                       set_array_nth(sample.untagged(),6,callstack.value());
 
                        set_array_nth(samples_array.untagged(),to_i,sample.value());
                }
@@ -158,6 +162,8 @@ void factor_vm::enqueue_safepoint_sample(cell samples, cell pc, bool foreign_thr
                else {
                        if (atomic::load(&current_gc_p))
                                atomic::add(&safepoint_sample_counts.gc_sample_count, samples);
+                       if (atomic::load(&current_jit_count) > 0)
+                               atomic::add(&safepoint_sample_counts.jit_sample_count, samples);
                        if (pc != 0 && !code->seg->in_segment_p(pc))
                                atomic::add(&safepoint_sample_counts.foreign_sample_count, samples);
                }
index 48bae883d1bb818a1293ae04e93a7e326fb66752..11319615cf9f964bc84a821f3af0cfbbc4700a47 100644 (file)
@@ -7,6 +7,8 @@ struct profiling_sample_count
        fixnum sample_count;
        // Number of samples taken during GC
        fixnum gc_sample_count;
+       // Number of samples taken during unoptimized compiler
+       fixnum jit_sample_count;
        // Number of samples taken during foreign code execution
        fixnum foreign_sample_count;
        // Number of samples taken during code execution in non-Factor threads
@@ -15,15 +17,18 @@ struct profiling_sample_count
        profiling_sample_count() :
                sample_count(0),
                gc_sample_count(0),
+               jit_sample_count(0),
                foreign_sample_count(0),
                foreign_thread_sample_count(0) {}
 
        profiling_sample_count(fixnum sample_count,
                fixnum gc_sample_count,
+               fixnum jit_sample_count,
                fixnum foreign_sample_count,
                fixnum foreign_thread_sample_count) :
                sample_count(sample_count),
                gc_sample_count(gc_sample_count),
+               jit_sample_count(jit_sample_count),
                foreign_sample_count(foreign_sample_count),
                foreign_thread_sample_count(foreign_thread_sample_count) {}
        
@@ -31,6 +36,7 @@ struct profiling_sample_count
        {
                return sample_count
                        + gc_sample_count
+                       + jit_sample_count
                        + foreign_sample_count
                        + foreign_thread_sample_count == 0;
        }
index ecc3359e0cebe23193ea5ae7f6ebd642c9bac11d..c17fb8c4efa6763eec48496ae2a77e9fcc683f84 100755 (executable)
--- a/vm/vm.cpp
+++ b/vm/vm.cpp
@@ -13,7 +13,7 @@ factor_vm::factor_vm() :
        gc_off(false),
        current_gc(NULL),
        current_gc_p(false),
-       current_jit_p(false),
+       current_jit_count(0),
        gc_events(NULL),
        fep_p(false),
        fep_help_was_shown(false),
index b62ac3f9c969dff850c221de0121ea8572ca09b1..dddb3ea8c12fd782cad5188985c53090d3b917ae 100755 (executable)
--- a/vm/vm.hpp
+++ b/vm/vm.hpp
@@ -95,7 +95,7 @@ struct factor_vm
        volatile cell current_gc_p;
 
        /* Set if we're in the jit */
-       volatile cell current_jit_p;
+       volatile fixnum current_jit_count;
 
        /* Mark stack */
        std::vector<cell> mark_stack;