TMemStatHelpers.cxx

Go to the documentation of this file.
00001 // @(#)root/memstat:$Name$:$Id: TMemStatHelpers.cxx 24366 2008-06-19 09:50:29Z anar $
00002 // Author: Anar Manafov (A.Manafov@gsi.de) 09/05/2008
00003 
00004 /*************************************************************************
00005  * Copyright (C) 1995-2008, Rene Brun and Fons Rademakers.               *
00006  * All rights reserved.                                                  *
00007  *                                                                       *
00008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
00009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
00010  *************************************************************************/
00011 
00012 // STD
00013 #include <iomanip>
00014 #include <sstream>
00015 // ROOT
00016 #include "Rtypes.h"
00017 // Memstat
00018 #include "TMemStatHelpers.h"
00019 
00020 using namespace std;
00021 
00022 //______________________________________________________________________________
00023 string Memstat::dig2bytes(Long64_t bytes)
00024 {
00025    // This function creates a string representation of the number of bytes,
00026    // represented as a number in B, kB, MB or GB depending on the value.
00027    // The result is rounded to a sensible number of digits
00028 
00029    ostringstream ss;
00030    ss << fixed;
00031 
00032    if (bytes < 0) {
00033       ss << '-';
00034       bytes = -bytes;
00035    }
00036 
00037    static const long kB = 1024L;
00038    static const long lMB = kB * kB;
00039    static const long lGB = lMB * kB;
00040 
00041    if (bytes < kB)
00042       ss << bytes << " B";
00043    else if (bytes < (10L * kB))
00044       ss << setprecision(2) << ((double)bytes / (float)kB) << " kB";
00045    else if (bytes < (100L * kB))
00046       ss << setprecision(1) << ((double)bytes / (float)kB) << " kB";
00047    else if (bytes < lMB)
00048       ss << setprecision(0) << ((double)bytes / (float)kB) << " kB";
00049    else if (bytes < (10L * lMB))
00050       ss << setprecision(2) << ((double)bytes / (float)lMB) << " MB";
00051    else if (bytes < (100L * lMB))
00052       ss << setprecision(1) << ((double)bytes / (float)lMB) << " MB";
00053    else if (bytes < lGB)
00054       ss << setprecision(0) << ((double)bytes / (float)lMB) << " MB";
00055    else
00056       ss << setprecision(2) << ((double)bytes / (float)lGB) << " GB";
00057 
00058    return ss.str();
00059 }

Generated on Tue Jul 5 14:44:00 2011 for ROOT_528-00b_version by  doxygen 1.5.1