TMemStatHelpers.cxx

Go to the documentation of this file.
00001 // @(#)root/memstat:$Id: TMemStatHelpers.cxx 32670 2010-03-18 10:54:04Z anar $
00002 // Author: Anar Manafov (A.Manafov@gsi.de) 2008-03-02
00003 
00004 /*************************************************************************
00005 * Copyright (C) 1995-2010, 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 // STD
00012 #include <iomanip>
00013 #include <sstream>
00014 // ROOT
00015 #include "Rtypes.h"
00016 // Memstat
00017 #include "TMemStatHelpers.h"
00018 
00019 using namespace std;
00020 
00021 //______________________________________________________________________________
00022 string memstat::dig2bytes(Long64_t bytes)
00023 {
00024    // This function creates a string representation of the number of bytes,
00025    // represented as a number in B, kB, MB or GB depending on the value.
00026    // The result is rounded to a sensible number of digits
00027 
00028    ostringstream ss;
00029    ss << fixed;
00030 
00031    if(bytes < 0) {
00032       ss << '-';
00033       bytes = -bytes;
00034    }
00035 
00036    static const long kB = 1024L;
00037    static const long lMB = kB * kB;
00038    static const long lGB = lMB * kB;
00039 
00040    if(bytes < kB)
00041       ss << bytes << " B";
00042    else if(bytes < (10L * kB))
00043       ss << setprecision(2) << ((double)bytes / (float)kB) << " kB";
00044    else if(bytes < (100L * kB))
00045       ss << setprecision(1) << ((double)bytes / (float)kB) << " kB";
00046    else if(bytes < lMB)
00047       ss << setprecision(0) << ((double)bytes / (float)kB) << " kB";
00048    else if(bytes < (10L * lMB))
00049       ss << setprecision(2) << ((double)bytes / (float)lMB) << " MB";
00050    else if(bytes < (100L * lMB))
00051       ss << setprecision(1) << ((double)bytes / (float)lMB) << " MB";
00052    else if(bytes < lGB)
00053       ss << setprecision(0) << ((double)bytes / (float)lMB) << " MB";
00054    else
00055       ss << setprecision(2) << ((double)bytes / (float)lGB) << " GB";
00056 
00057    return ss.str();
00058 }

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