/////////////////////////////////////////////////////////////// // // CPerfStatServerInfoImpl::GetProcessMemoryUsage // // // /////////////////////////////////////////////////////////////// SString CPerfStatServerInfoImpl::GetProcessMemoryUsage( void ) { #ifdef WIN32 PROCESS_MEMORY_COUNTERS psmemCounter; BOOL bResult = GetProcessMemoryInfo( GetCurrentProcess(), &psmemCounter, sizeof( PROCESS_MEMORY_COUNTERS ) ); if ( !bResult ) return ""; uint uiMB = psmemCounter.WorkingSetSize / (long long)( 1024 * 1024 ); return SString( "%d MB", uiMB ); #else // 'file' stat seems to give the most reliable results std::ifstream statStream( "/proc/self/stat", std::ios_base::in ); // dummy vars for leading entries in stat that we don't care about std::string pid, comm, state, ppid, pgrp, session, tty_nr; std::string tpgid, flags, minflt, cminflt, majflt, cmajflt; std::string utime, stime, cutime, cstime, priority, nice; std::string O, itrealvalue, starttime; // the two fields we want unsigned long vsize; long rss; statStream >> pid >> comm >> state >> ppid >> pgrp >> session >> tty_nr >> tpgid >> flags >> minflt >> cminflt >> majflt >> cmajflt >> utime >> stime >> cutime >> cstime >> priority >> nice >> O >> itrealvalue >> starttime >> vsize >> rss; // don't care about the rest statStream.close(); long page_size_kb = sysconf( _SC_PAGE_SIZE ) / 1024; // in case x86-64 is configured to use 2MB pages uint vm_usage = vsize / ( 1024 * 1024 ); uint resident_set = rss * page_size_kb / 1024; return SString( "VM:%d MB RSS:%d MB", vm_usage, resident_set ); #endif }
int main(int argc, char **argv) { float publish_rate = 1; float shutdown_after_sending = 10; float message_payload_size = 0; long int sent_count = 0; char* message_string; ros::init(argc, argv, "talker"); ros::NodeHandle nh; pubsub::latency_test_message msg; ros::Publisher chatter_pub = nh.advertise<pubsub::latency_test_message>("chatter", 1000); ros::param::get("~/publish_rate", publish_rate); ros::param::get("~/shutdown_after_sending", shutdown_after_sending); ros::param::get("~/message_payload_size", message_payload_size); if (message_payload_size > 0) { message_string = (char *)malloc((size_t)message_payload_size); for (long int i=0; i < message_payload_size; i++) { message_string[i] = i%10 + '0'; } msg.payload.append(message_string); free(message_string); } ros::Rate loop_rate((double)publish_rate); ROS_INFO("Publisher ('talker') loop starts now with publish rate %ld", (long int)publish_rate); ROS_INFO("Publisher ('talker') will shutdown after publishing %ld messages", (long int)shutdown_after_sending); ROS_INFO("Publisher ('talker') will publish messages with a size of %ld bytes", (long int)message_payload_size); while (ros::ok() && sent_count < (long int)shutdown_after_sending) { msg.header.seq = sent_count; // begin vmem size code // source copied from MIRA benchmark and adapted for re-use here std::ifstream statStream("/proc/self/status", std::ios_base::in); std::string line; bool vmem_found = false; while(!std::getline(statStream, line).eof() && !vmem_found) { // format of line we are looking for: // VmSize: 28812 kB if (line.find("VmSize") != std::string::npos) { vmem_found = true; sscanf(line.c_str(),"VmSize:%ld kB", &(msg.virtual_memory_size)); // important note: a throttled console message uses quite some extra cpu power, so // be aware of that when you turn debug on using e.g. rqt_logger_level ROS_DEBUG_THROTTLE(1.0,">>>> sscanf of line %s gives pub virtual_memory_size %ld", line.c_str(), msg.virtual_memory_size); } } // end vmem size code ros::WallTime wallTime = ros::WallTime::now(); msg.header.stamp.sec = wallTime.sec; msg.header.stamp.nsec = wallTime.nsec; chatter_pub.publish(msg); sent_count++; loop_rate.sleep(); } ros::shutdown(); return 0; } //main