OLD | NEW |
---|---|
1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 1046 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1057 CheckChildrenNames(root, names); | 1057 CheckChildrenNames(root, names); |
1058 | 1058 |
1059 const v8::CpuProfileNode* startNode = | 1059 const v8::CpuProfileNode* startNode = |
1060 GetChild(env->GetIsolate(), root, "start"); | 1060 GetChild(env->GetIsolate(), root, "start"); |
1061 GetChild(env->GetIsolate(), startNode, "foo"); | 1061 GetChild(env->GetIsolate(), startNode, "foo"); |
1062 | 1062 |
1063 profile->Delete(); | 1063 profile->Delete(); |
1064 } | 1064 } |
1065 | 1065 |
1066 | 1066 |
1067 static i::Code* GetFunctionCodeFromHeap(i::Isolate* isolate, | |
1068 const char* name) { | |
1069 CHECK_NE(NULL, isolate); | |
1070 i::Heap* heap = isolate->heap(); | |
1071 CHECK_NE(NULL, heap); | |
1072 | |
1073 heap->CollectAllGarbage(Heap::kMakeHeapIterableMask, | |
1074 "Logger::LogCompiledFunctions"); | |
yurys
2014/08/08 08:13:23
"GetFunctionCodeFromHeap"
| |
1075 | |
1076 i::HandleScope scope(isolate); | |
1077 i::HeapIterator iterator(heap); | |
1078 i::DisallowHeapAllocation no_gc; | |
1079 | |
1080 // Iterate the heap to find shared function info objects. | |
1081 for (i::HeapObject* obj = iterator.next(); | |
1082 obj != NULL; | |
1083 obj = iterator.next()) { | |
1084 if (!obj->IsSharedFunctionInfo()) continue; | |
1085 i::SharedFunctionInfo* sfi = i::SharedFunctionInfo::cast(obj); | |
1086 if (sfi->is_compiled() | |
1087 && (!sfi->script()->IsScript() | |
1088 || i::Script::cast(sfi->script())->HasValidSource())) { | |
1089 i::Handle<i::String> func_name(sfi->DebugName()); | |
1090 i::SmartArrayPointer<char> str = | |
1091 sfi->DebugName()->ToCString(i::DISALLOW_NULLS, | |
1092 i::ROBUST_STRING_TRAVERSAL); | |
1093 if (strcmp(str.get(), name) == 0) | |
1094 return sfi->code(); | |
1095 } | |
1096 } | |
1097 | |
1098 return NULL; | |
1099 } | |
1100 | |
1101 | |
1102 // This tests checks distribution of the samples through the source lines. | |
1103 TEST(TickLines) { | |
1104 CcTest::InitializeVM(); | |
1105 LocalContext env; | |
1106 i::Isolate* isolate = CcTest::i_isolate(); | |
1107 CpuProfiler* profiler = isolate->cpu_profiler(); | |
1108 i::HandleScope scope(isolate); | |
1109 | |
1110 i::EmbeddedVector<char, 512> source; | |
1111 const char* foo_name = "foo"; | |
1112 i::SNPrintF(source, | |
1113 "(function %s(timeout) {\n" | |
1114 " this.mmm = 0;\n" | |
1115 " var start = Date.now();\n" | |
1116 " while (Date.now() - start < timeout) {\n" | |
1117 " var n = 100*1000;\n" | |
1118 " while(n > 1) {\n" | |
1119 " n--;\n" | |
1120 " this.mmm += n * n * n;\n" | |
1121 " }\n" | |
1122 " }\n" | |
1123 "})(this)\n", foo_name); | |
yurys
2014/08/08 08:13:23
'this' passed as timeout looks like an error.
| |
1124 | |
1125 // Compile the source. The compiled functions with relocation info are | |
1126 // located in the heap. | |
1127 CompileRun(source.start()); | |
1128 | |
1129 // The profiler consumes code creation events for all compiled functions | |
1130 // found in the heap. | |
1131 profiler->StartProfiling("", false); | |
1132 | |
1133 // Enqueue the tick events to add 'foo' function to the profile. | |
1134 i::Code* foo_code = GetFunctionCodeFromHeap(isolate, foo_name); | |
1135 CHECK_NE(NULL, foo_code); | |
1136 i::Address address = foo_code->instruction_start(); | |
1137 CHECK_NE(NULL, address); | |
1138 EnqueueTickSampleEvent(profiler->processor(), address); | |
1139 v8::base::OS::Sleep(100); // Ensure that a new node is added to the profile. | |
yurys
2014/08/08 08:13:23
We need to use some explicit event instead of Slee
| |
1140 | |
1141 // Get a code entry for 'foo' function. | |
1142 ProfileGenerator* generator = profiler->generator(); | |
1143 CodeEntry* foo = generator->code_map()->FindEntry(address); | |
yurys
2014/08/08 08:13:23
This is not thread-safe as code_map is used on the
| |
1144 CHECK_NE(NULL, foo); | |
1145 | |
1146 CpuProfile* profile = profiler->StopProfiling(""); | |
1147 CHECK_NE(NULL, profile); | |
1148 | |
1149 // Firstly, check the underlying code entry for 'foo' function. | |
1150 CHECK_EQ(foo_name, foo->name()); | |
1151 const i::JITLineInfoTable* foo_line_info = foo->line_info(); | |
1152 CHECK_NE(NULL, foo_line_info); | |
1153 CHECK_EQ(false, foo_line_info->Empty()); | |
1154 | |
1155 // Secondly, check the hit source lines are accessible using V8 Public APIs. | |
1156 const i::ProfileTree* tree = profile->top_down(); | |
1157 ProfileNode* root = tree->root(); | |
1158 CHECK_NE(NULL, root); | |
1159 ProfileNode* foo_node = root->FindChild(foo); | |
1160 CHECK_NE(NULL, foo_node); | |
1161 | |
1162 // Add 10 faked ticks to source line #5. | |
1163 int hit_line = 5; | |
1164 int hit_count = 10; | |
1165 for (int i = 0; i < hit_count; i++) | |
1166 foo_node->IncrementLineTicks(hit_line); | |
1167 | |
1168 unsigned int line_count = foo_node->GetHitLineCount(); | |
1169 CHECK_EQ(2, line_count); // Expect two hit source lines - #1 and #5. | |
1170 ScopedVector<v8::CpuProfileNode::LineTick> entries(line_count); | |
1171 CHECK_EQ(true, foo_node->GetLineTicks(&entries[0], line_count)); | |
1172 int value = 0; | |
1173 for (int i = 0; i < entries.length(); i++) | |
1174 if (entries[i].line == hit_line) { | |
1175 value = entries[i].hit_count; | |
1176 break; | |
1177 } | |
1178 CHECK_EQ(hit_count, value); | |
1179 } | |
1180 | |
1181 | |
1067 static const char* call_function_test_source = "function bar(iterations) {\n" | 1182 static const char* call_function_test_source = "function bar(iterations) {\n" |
1068 "}\n" | 1183 "}\n" |
1069 "function start(duration) {\n" | 1184 "function start(duration) {\n" |
1070 " var start = Date.now();\n" | 1185 " var start = Date.now();\n" |
1071 " while (Date.now() - start < duration) {\n" | 1186 " while (Date.now() - start < duration) {\n" |
1072 " try {\n" | 1187 " try {\n" |
1073 " bar.call(this, 10 * 1000);\n" | 1188 " bar.call(this, 10 * 1000);\n" |
1074 " } catch(e) {}\n" | 1189 " } catch(e) {}\n" |
1075 " }\n" | 1190 " }\n" |
1076 "}"; | 1191 "}"; |
(...skipping 599 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1676 inner_profile = NULL; | 1791 inner_profile = NULL; |
1677 CHECK_EQ(0, iprofiler->GetProfilesCount()); | 1792 CHECK_EQ(0, iprofiler->GetProfilesCount()); |
1678 | 1793 |
1679 v8::CpuProfile* outer_profile = profiler->StopProfiling(outer); | 1794 v8::CpuProfile* outer_profile = profiler->StopProfiling(outer); |
1680 CHECK(outer_profile); | 1795 CHECK(outer_profile); |
1681 CHECK_EQ(1, iprofiler->GetProfilesCount()); | 1796 CHECK_EQ(1, iprofiler->GetProfilesCount()); |
1682 outer_profile->Delete(); | 1797 outer_profile->Delete(); |
1683 outer_profile = NULL; | 1798 outer_profile = NULL; |
1684 CHECK_EQ(0, iprofiler->GetProfilesCount()); | 1799 CHECK_EQ(0, iprofiler->GetProfilesCount()); |
1685 } | 1800 } |
OLD | NEW |