From 2d269859d173fb087c94aec9e9b3f09650cd36ff Mon Sep 17 00:00:00 2001 From: Markus Stange Date: Thu, 22 Mar 2012 23:40:33 +0100 Subject: [PATCH] Bug 733861 - Use std::ostreams for profile stringification. r=BenWa --- tools/profiler/TableTicker.cpp | 80 +++++++++++++++------------------- 1 file changed, 34 insertions(+), 46 deletions(-) diff --git a/tools/profiler/TableTicker.cpp b/tools/profiler/TableTicker.cpp index 102722245cc..fdec54b17fa 100644 --- a/tools/profiler/TableTicker.cpp +++ b/tools/profiler/TableTicker.cpp @@ -38,13 +38,15 @@ #include #include +#include +#include +#include #include "sps_sampler.h" #include "platform.h" #include "nsXULAppAPI.h" #include "nsThreadUtils.h" #include "prenv.h" #include "shared-libraries.h" -#include "mozilla/StringBuilder.h" #include "mozilla/StackWalk.h" #include "JSObjectBuilder.h" @@ -147,7 +149,7 @@ public: , mTagName(aTagName) { } - string TagToString(ThreadProfile *profile); + friend std::ostream& operator<<(std::ostream& stream, const ProfileEntry& entry); private: friend class ThreadProfile; @@ -258,14 +260,7 @@ public: mWritePos = mLastFlushPos; } - void ToString(StringBuilder &profile) - { - int readPos = mReadPos; - while (readPos != mLastFlushPos) { - profile.Append(mEntries[readPos].TagToString(this).c_str()); - readPos = (readPos + 1) % mEntrySize; - } - } + friend std::ostream& operator<<(std::ostream& stream, const Profile& profile); JSObject *ToJSObject(JSContext *aCx) { @@ -309,16 +304,6 @@ public: return profile; } - void WriteProfile(FILE* stream) - { - int readPos = mReadPos; - while (readPos != mLastFlushPos) { - string tag = mEntries[readPos].TagToString(this); - fwrite(tag.data(), 1, tag.length(), stream); - readPos = (readPos + 1) % mEntrySize; - } - } - ProfileStack* GetStack() { return mStack; @@ -431,10 +416,11 @@ public: } #endif - FILE* stream = ::fopen(buff, "w"); - if (stream) { - t->GetPrimaryThreadProfile()->WriteProfile(stream); - ::fclose(stream); + std::ofstream stream; + stream.open(buff); + if (stream.is_open()) { + stream << *(t->GetPrimaryThreadProfile()); + stream.close(); LOG("Saved to " FOLDER "profile_TYPE_PID.txt"); } else { LOG("Fail to open profile log file."); @@ -675,31 +661,32 @@ void TableTicker::Tick(TickSample* sample) } } -string ProfileEntry::TagToString(ThreadProfile *profile) +std::ostream& operator<<(std::ostream& stream, const Profile& profile) { - string tag = ""; - if (mTagName == 'r') { - char buff[50]; - snprintf(buff, 50, "%-40f", mTagFloat); - tag += string(1, mTagName) + string("-") + string(buff) + string("\n"); - } else if (mTagName == 'l') { - char tagBuff[1024]; - Address pc = mTagAddress; - snprintf(tagBuff, 1024, "l-%p\n", pc); - tag += string(tagBuff); + int readPos = profile.mReadPos; + while (readPos != profile.mLastFlushPos) { + stream << profile.mEntries[readPos]; + readPos = (readPos + 1) % profile.mEntrySize; + } + return stream; +} + +std::ostream& operator<<(std::ostream& stream, const ProfileEntry& entry) +{ + if (entry.mTagName == 'r') { + stream << entry.mTagName << "-" << std::fixed << entry.mTagFloat << "\n"; + } else if (entry.mTagName == 'l') { + stream << entry.mTagName << "-" << static_cast(entry.mTagData) << "\n"; } else { - tag += string(1, mTagName) + string("-") + string(mTagData) + string("\n"); + stream << entry.mTagName << "-" << entry.mTagData << "\n"; } #ifdef ENABLE_SPS_LEAF_DATA - if (mLeafAddress) { - char tagBuff[1024]; - unsigned long pc = (unsigned long)mLeafAddress; - snprintf(tagBuff, 1024, "l-%llu\n", pc); - tag += string(tagBuff); + if (entry.mLeafAddress) { + stream << entry.mTagName << "-" << entry.mLeafAddress << "\n"; } #endif - return tag; + return stream; } void mozilla_sampler_init() @@ -767,11 +754,12 @@ char* mozilla_sampler_get_profile() return NULL; } - StringBuilder profile; - t->GetPrimaryThreadProfile()->ToString(profile); + std::stringstream profile; + profile << *(t->GetPrimaryThreadProfile()); - char *rtn = (char*)malloc( (profile.Length()+1) * sizeof(char) ); - strcpy(rtn, profile.Buffer()); + std::string profileString = profile.str(); + char *rtn = (char*)malloc( (profileString.length() + 1) * sizeof(char) ); + strcpy(rtn, profileString.c_str()); return rtn; }