measure CPU usage per second of a dynamically linked library - c++

I have a sample application which uses a dynamically linked library library.so. I was measuring CPU usage of the sample application with the top command. But it shows CPU usage of both sample app and library.so per second. But I want to see the CPU usage of only the library.so. Is there anyway to do this? I heard its achievable with htop but could not find out how. I used the tree view but it shows several processes as the sample app process. I could not understand which one is library.so. I am using centos 5.11. Kernel version 3.2.63-1.el5.elrepo.

Given the library is considered part of your program, one way would be to implement the measurement within your code. The following minimal example is implemented on C++11 running only one function from a hypothetical library:
#include <chrono>
#include <iostream>
#include <hypothetical>
int main() {
using namespace std::chrono;
system_clock systemClock;
system_clock::time_point startingTime{systemClock.now()};
hypothetical::function();
system_clock::duration libraryTime{systemClock.now() - startingTime};
std::cout << "Hypothetical library took " << duration_cast<seconds>(libraryTime).count() << " seconds to run.\n";
return 0;
}
You will need to extend this to all of the functions that your program invokes from your library.

Related

How to read data in a specific memory address in c++

I have created an integer variable using the following code in first.cpp:
#include <iostream>
using namespace std;
int main()
{
int myvar = 10;
cout << &myvar;
// I only need two steps above.
// The following steps are coded to make this program run continuously.
cout << "Enter your name" << endl;
string name;
cin >> name;
return 0;
}
Output of first.cpp is:
>0x6dfed4
While the above program is running, I also run the following program in second.cpp:
#include <iostream>
using namespace std;
int main()
{
// I this program I want to read the content in myvar variable in first.cpp
// How to do it ?
}
Using the second program, I want to read the content of the myvar variable in first.cpp.
How to do it ?
Thank you.
To elaborate on my comment above:
As I wrote each program will run in a different process.
Each process has a separate adddress space.
Therefore using the address of a variable in another process doesn't make any sense.
In order to communicate between processes, you need some kind of IPC (inter-process communication):
Inter-process communication
If you only need to share a variable, the first mechanism that comes to mind is to use shared memory:
Shared memory
Shared-memory is very much OS dependent.
You didn't mention which OS you are using.
On Windows you can use Win API for managing shared memory:
Windows shared memory
There's an equivalent on Linux, but I am not familiar with the details.
Alternatively you can use the boost solution which is cross platform:
boost shared memory
If you'll delve into that it will be clear that these kind of solutions comes with some compilcations.
So the question is why do you need to do it ? Maybe there's a better solution to your problem
(you haven't described what it actually is, so there's not much more I can say).

Memory issue with threads (tinythread, C++)

I debug a strange memory issue: When a multithreaded algorithm runs in a loop its memory consumption increases with every iteration although the heap checker of of GooglePerformanceTools says there is no leak. Finally I have made a separate minimal program that reproduces the bug. It seems that the threads are the problem:
#include <stdio.h>
#include <iostream>
#include <vector>
#include "tinythread.h"
using namespace std;
int a(0);
void doNothingAtAll(void*)
{
++a;
}
void startAndJoin100()
{
vector<tthread::thread*> vThreads;
for(int i=0;i<100;++i)
{
vThreads.push_back(new tthread::thread(doNothingAtAll,NULL));
}
while(!vThreads.empty())
{
tthread::thread* pThread(vThreads.back());
pThread->join();
delete pThread;
vThreads.pop_back();
}
}
int main()
{
for(int i=0;i<10;++i)
{
cout<<"calling startAndJoin100()"<<endl;
startAndJoin100();
cout<<"all threads joined"<<endl;
cin.get();
}
return 0;
}
main() calls 10 times startAndJoin100(). It waits for a key stroke after each iteration so that one can take the memory consumption which is (under Ubuntu 17.10, 64-bit):
VIRT
2.1 GB
4 GB
5.9 GB
7.8 GB
9.6 GB
11.5 GB
13.4 GB
15.3 GB
17.2 GB
19.0 GB
Note: C++11 can't be used and the program must compile on Linux and Windows, thus tinythread is used. Minimal test code with Makefile:
geom.at/_downloads/testTinyThread.zip
I answer my own question, this may be useful for somebody later:
Conclusion:
1) I'd really like to keep TinyThread because C++11 is unavailable (VS2008 and old Linux Systems must be supported) and no additional library shall be linked (TinyThread consists only of an *.h and *.cpp file while Boost and other solutions I know require linking a DLL).
2) Valgrind and the heap checker of the GooglePerformanceTools do not report memory leaks and I have looked into the code - it seems to be correct although the virtual memory consumption increases drastically in the minimal example posted above. It seems that the system does not re-use the previously assigned memory pages and I have not found an explanation for this behavior. Thus I do not blame TinyThread++ but it works when pthreads are used directly instead.
3) The workaround: There is a C alternative called TinyCThread: https://tinycthread.github.io/ that works also for C++ and it does not cause the problems observed with TinyThread++.

Linux/crossplatform API for timezone rules? (replace locking localtime_r)

We have some code that wants to call localtime very often from multiple threads. (Relevant background: it's a server where one of the things you can ask it for is the local time as a string, and it wants to be able to serve 100Ks of requests per second.)
We have discovered that on Ubuntu Linux 12.04, the glibc function localtime_r ("reentrant localtime") calls __tz_convert, which still takes a global lock!
(Also, it looks like FreeBSD makes localtime_r call tzset on every single invocation, because they're paranoid that the program might have done a setenv("TZ") and/or the user downloaded a new version of /etc/localtime between now and the last time localtime_r was called. (This is the opposite of the situation described here; it seems that glibc calls tzset on every invocation of localtime but not localtime_r, just to be confusing.)
Obviously, this is terrible for performance. For our purposes, we would like to basically "snapshot" the rules for our current timezone when the server starts running, and then use that snapshot forever afterward. So we'd continue to respect Daylight Savings Time rules (because the rules for when to switch to DST would be part of the snapshot), but we would never go back to the disk, take mutexes, or do anything else that would cause threads to block. (We are fine with not respecting downloaded updates to tzinfo and not respecting changes to /etc/localtime; we don't expect the server to physically change timezones while it's running.)
However, I can't find any information online about how to deal with timezone rules — whether there's a userspace API for working with them, or whether we'll be forced to reimplement a few hundred lines of glibc code to read the timezone data ourselves.
Do we have to reimplement everything downstream of __tz_convert — including tzfile_read, since it doesn't seem to be exposed to users? Or is there some POSIX interface and/or third-party library that we could use for working with timezone rules?
(I've seen http://www.iana.org/time-zones/repository/tz-link.html but I'm not sure it's helpful.)
Use https://github.com/google/cctz
It's fast and should do everything you want with a very simple API.
In particular, for a cctz equivalent to localtime, use the cctz::BreakTime() function. For example, https://github.com/google/cctz/blob/master/examples/example3.cc
Perhaps this free, open-source timezone library would fit the need.
It has a configuration flag called LAZY_INIT which is fully documented here. By default it is on, and will cause calls to std::call_once the first time each individual timezone is accessed. However you can compile with:
-DLAZY_INIT=0
and then the calls to std::call_once go away. Every single timezone is read from disk and fully initialized on first access (via a function local static). From then on, things are stable and lock-free, with no disk access. Naturally this increases initialization time up front, but decreases the "first-access" time for each timezone.
This library requires C++11/14, and so may not be suitable for that reason. It is based on (and heavily uses) the C++11 <chrono> library. Here is sample code that prints out the current local time:
#include "tz.h"
#include <iostream>
int
main()
{
using namespace date;
auto local = make_zoned(current_zone(), std::chrono::system_clock::now());
std::cout << local << '\n';
}
which just output for me:
2016-04-12 10:13:14.585945 EDT
The library is a modern, high-performance thread safe design. It is also very flexible and fully documented. Its capabilities go far beyond a simple replacement for C's localtime. Unlike the C API, you can specify any IANA timezone you need, for example:
auto local = make_zoned("Europe/London", std::chrono::system_clock::now());
This gives the current time in London:
2016-04-12 15:19:59.035533 BST
Note that by default the precision of the timestamp is that of std::chrono::system_clock. If you prefer another precision, that is easily accomplished:
using namespace date;
using namespace std::chrono;
auto local = make_zoned("Europe/London", std::chrono::system_clock::now());
std::cout << format("%F %H:%M %Z", local) << '\n';
2016-04-12 15:22 BST
See the docs for more details.

How can I use boost::thread::timed_join with nanoseconds enabled in boost::date_time?

Here is some C++ code illustrating my problem with a minimal expample:
// uncomment the next line, to make it hang up:
//#define BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG //needed for nanosecond support of boost
#include <boost/thread.hpp>
void foo()
{
while(true);
}
int main(int noParameters, char **parameterArray)
{
boost::thread MyThread(&foo);
if ( MyThread.timed_join( boost::posix_time::seconds(1) ) )
{
std::cout<<"\nDone!\n";
}
else
{
std::cerr<<"\nTimed out!\n";
}
}
As long as I don't turn on the nanosecond support everthing works as expected, but as soon as I uncomment the #define needed for the nanosecond support in boost::posix_time the program doesn't get past the if-statement any more, just as if I had called join() instead of timed_join().
Now I've already figured out, that this happens because BOOST_DATE_TIME_POSIX_TIME_STD_CONFIG changes the actual data representation of the timestamps from a single 64bit integer to 64+32 bit. A lot boost stuff is completely implemented inside the headers but the thread methods are not and because of that they cannot adapt to the new data format without compiling them again with the apropriate options. Since the code is meant to run on an external server, compiling my own version of boost is not an option and neither is turning off the nanosecond support.
Therefore my question is as follows: Is there a way to pass on a value (on the order of seconds) to timed_join() without using the incompatible 96bit posix_time methods and without modifying the standard boost packages?
I'm running on Ubuntu 12.04 with boost 1.46.1.
Unfortunately I don't think your problem can be cleanly solved as written. Since the library you're linking against was compiled without nanosecond support, by definition you violate the one-definition rule if you happen to enable nanosecond support for any piece that's already compiled into the library binary. In this case, you're enabling it across the function calls to timed_join.
The obvious solution is to decide which is less painful to give up: Building your own boost, or removing nanosecond times.
The less obvious "hack" that may or may not totally work is to write your own timed_join wrapper that takes a thread object and an int representing seconds or ms or whatever. Then this function is implemented in a source file with nothing else and that does not enable nanosecond times for the specific purpose of calling into the compiled boost binary. Again I want to stress that if at any point you fail to completely segregate such usages you'll violate the one definition rule and run into undefined behavior.

C++, shared library, "unit tests" in main function

Background: I have a complicated application that I have inherited and am extending (graduate research project). I want to make sure my re-organizations have a positive long term effect on maintainability and usability -- in other words I want to make sure I set things up in as standard a way as possible for future people who might work on the code. I do not have time, nor is it my place, to completely re-structure the application.
The application is an ecosystem model. It consists of a shared library written in C++ that does the heavy lifting. The library is included in a Java application that "drives" the simulation -- handles some database I/O and provides a GUI.
In the course of my development, I have added a number of utility functions to the project, mostly for convenient printing of internal variables to the console (directly from the C++ portions of the code) during run-time.
I am now factoring these functions out into a utility file (header and cpp) that is included in other files as needed. I put the functions in their own namespace following an example here: Organising utility functions in C++
Question: If I want to write some tests for the utility functions so that I can develop and experiment w/o re-compiling and modifying/running the entire model, where and how should these tests be best included?
Will it be a problem if I have a main() function in the util.cpp file? To my surprise, I tried this and it works; I can compile and run the util.cpp file independently. Also the primary application which includes util.cpp still compiles and runs fine. I was surprised because I thought the presence of a second main() would be a problem -- although the application entry point is in the java code.
However I am not sure if this is the best route; I don't have enough experience to see future pitfalls with this tactic.
Here is a short example of the my util.cpp file:
#include "util.hpp"
#include <iostream>
#include <vector>
namespace util {
/** Prints a std::vector of doubles in a format that can be
* copied directly into a python console. */
void util::pyprint_vec(const std::vector<double> & v){
std::cout << "[";
for(std::vector<double>::const_iterator it = v.begin(); it != v.end(); ++it){
std::cout << *it << ", ";
}
std::cout << "\b\b]"; // remove the last comma
}
}
int main() {
using namespace util;
using namespace std;
cout << "Testing some of the utility functions...\n";
vector<double> a_vec(50,12.0);
pyprint_vec(a_vec);
cout << endl;
return 0;
}
Ultimately I envision templating some of the functions (at which point they actually move to util.hpp) and adding to the file as well as being able to use it in other projects.
Thanks in advance for any advice.
Normally, you would want to write a seperate executable program that includes the unit tests and that links against the functions that are to be tested. For example, you could create a util_test.cpp that includes a main()-function and the code with the tests, and that *#include*s the util.hpp. When compiling, use static or dynamic linking to the code that you want to be tested.
Technicaly, it won't be a problem to have a main() function in the util.cpp. However, if you include a second main function somewhere else in the library (to unit-test something else), and link it into the same shared object, you will recieve linker errors.
The main function has no special meaning other than, when the linker creates the executable, it inserts special code so that when you start the program, the code from main gets executed. If you load a "shared object" library, you don't "start" the program, and as long as you don't call the main function explicitly that code won't get executed.
From a purely getting it working point of view, one main function is fine, but two is going to break your compilation as only one can be called at startup.
You really want to looking into a unit-testing framework, something like cppunit or cxxtest (my current favorite). These are going to provide functionality that you'll find yourself re-implementing poorly if you try to go it alone. Generating multiple executables is a losers game, it will take forever to compile if your codebase is of any size at all. You really want one compiled executable driven by some kind of framework.