[#47955] るびま記事募集:FiberとかSinatra/PadrinoとかBundlerとか — Makoto Kuwata <kwa@...>

桑田といいます。るびま編集部に入りました。

17 messages 2011/04/11

[#48016] 文字列を正規表現にマッチする部分と他の部分に分割 — "5.5" <5.5@...>

5.5 です。

12 messages 2011/04/28

[ruby-list:47962] Re: Ruby用のリアルタイムプロファイラ

From: "Shota Fukumori (sora_h)" <sorah@...>
Date: 2011-04-12 00:01:54 UTC
List: ruby-list #47962
sora_hです。

2011/4/12 Takahiro Sunaga <sunagae@sunagae.net>:
> 現在リアルタイムで動作するRuby用実行時間プロファイラを開発しています。
> まだまだ不安定ではありますが、ある程度使用できるものになりましたので以下
> のページで公開しています。
> http://sunagae.net/software/llprof

さっそく試してみました。

残念ながらOSXで動きません… ;'(

さっそくパッチを書いてみました。添付しておきます。

このパッチを適用することでOSX上で動く気がするんですけどテストコードなどが存在しなさそうなので
ちゃんと動くかは確認できてません。一応monitorからの接続と表示は確認したんですけど。

そして、この修正も間違ってるかもしれないのでいくつかご指摘いただければと思います。

現時点で感じた疑問点はこんな感じ:

・ライセンスは?
・class_table.cppのnull_strとかnullstrは__threadである必要はあるのかどうか分からなかった。

-- 
Shota Fukumori a.k.a. @sora_h - http://codnote.net/

Attachments (1)

osx.diff (2.85 KB, text/x-diff)
diff -r 3289e745236d rrprofext/call_tree.cpp
--- a/rrprofext/call_tree.cpp	Wed Apr 06 13:25:17 2011 +0900
+++ b/rrprofext/call_tree.cpp	Tue Apr 12 08:56:31 2011 +0900
@@ -183,8 +183,10 @@
 
 ThreadInfo *gFirstThread;
 ThreadInfo *gLastThread;
-__thread ThreadInfo *gCurrentThread = NULL;
+//__thread ThreadInfo *gCurrentThread = NULL;
+pthread_key_t threadKey;
 pthread_mutex_t gThreadDataMutex = PTHREAD_MUTEX_INITIALIZER;
+#define gCurrentThread ((ThreadInfo*)pthread_getspecific(threadKey))
 unsigned long long int gThreadMaxID = 0;
 
 ThreadInfo *get_current_thread()
@@ -192,7 +194,8 @@
     if(gCurrentThread != NULL)
         return gCurrentThread;
     pthread_mutex_lock(&gThreadDataMutex);
-    gCurrentThread = new ThreadInfo(gThreadMaxID);
+    //gCurrentThread = new ThreadInfo(gThreadMaxID);
+    pthread_setspecific(threadKey,new ThreadInfo(gThreadMaxID));
     gThreadMaxID++;
     
     if(!gLastThread)
@@ -594,6 +597,7 @@
     cout << "[Init]"  << endl << "  pNodes = " << gBackBuffer_SerializedNodeInfoArray << endl;
     cout << "  pStack = " << gBackBuffer_SerializedStackArray << endl;
 
+    pthread_key_create(&threadKey,NULL);
     get_current_thread();
    
 
diff -r 3289e745236d rrprofext/class_table.cpp
--- a/rrprofext/class_table.cpp	Wed Apr 06 13:25:17 2011 +0900
+++ b/rrprofext/class_table.cpp	Tue Apr 12 08:56:31 2011 +0900
@@ -32,7 +32,7 @@
     (st_index_t (*)(...)) clstbl_hash
 };
 
-__thread const char *nullstr = "(null)";
+const char *nullstr = "(null)";
 const char *new_str(const char *str)
 {
     if(!str)
@@ -68,7 +68,7 @@
     pthread_mutex_unlock(&mtx_);
 }
 
-__thread const char *null_str = "(ERR)";
+const char *null_str = "(ERR)";
 const char * NameTable::Get(Key entry)
 {
     char *name;
diff -r 3289e745236d rrprofext/rrprof.cpp
--- a/rrprofext/rrprof.cpp	Wed Apr 06 13:25:17 2011 +0900
+++ b/rrprofext/rrprof.cpp	Tue Apr 12 08:56:31 2011 +0900
@@ -6,6 +6,9 @@
 #include <unistd.h>
 #include <errno.h>
 
+#if (defined(__APPLE__) || defined(__NeXT__)) && defined(__MACH__)
+#include <mach/mach_time.h>
+#endif
 
 #include <pthread.h>
 #include <sys/types.h>
@@ -19,8 +22,6 @@
 #include "call_tree.h"
 #include "server.h"
 
-
-
 #define POLLFD_MAX	256
 
 
@@ -46,9 +47,19 @@
 */
 time_val_t GetNowTime_clock_gettime_monotonic()
 {
+	//clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
 	struct timespec tp;
-	//clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tp);
+#if (defined(__APPLE__) || defined(__NeXT__)) && defined(__MACH__)
+	//gettimeofday(&tp, NULL);
+	static mach_timebase_info_data_t ti = {0,0};
+	if(ti.denom == 0) mach_timebase_info(&ti); 
+	uint64_t nano = mach_absolute_time() * (ti.numer / ti.denom);
+
+	tp.tv_sec = nano * 1e-9;  
+        tp.tv_nsec = nano - (tp.tv_sec * 1e9);
+#else
 	clock_gettime(CLOCK_MONOTONIC, &tp);
+#endif
     
     // return (time_val_t)tp.tv_nsec;
 	return ((time_val_t)tp.tv_sec)*1000*1000*1000 + ((time_val_t)tp.tv_nsec);

In This Thread