I am working on a background program that will be running for a long time, and I have a external logging program (SmartInspect) that I want to feed with some values periodically, to monitor it in realtime when debugging.
I know I can simply fire up multiple programs, like the Task Manager, or IARSN TaskInfo, but I'd like to keep everything in my own program for this, as I also wants to add some simple rules like if the program uses more than X% CPU, flag this in the log.
I have a background thread that periodically feeds some statistics to SmartInspect, like memory consumption, working set, etc.
Is it possible for this thread to get a reasonably accurate measure of how much of the computer's CPU resources it consumes? The main program is a single-threaded application (apart from the watchdog thread that logs statistics) so if a technique is limited to how much does a single thread use then that would be good too.
I found some entries related to something called rusage for Linux and C. Is there something similar I can use for this?
Edit: Ok, I tried the performance counter way, but it added quite a lot of GC-data each time called, so the graph for memory usage and garbage collection skyrocketed. I guess I'll just leave this part out for now.
You can also use System.Diagnostics.Process.TotalProcessorTime and System.Diagnostics.ProcessThread.TotalProcessorTime properties to calculate your processor usage as this article describes.
Have a look at System.Diagnostics.PerformanceCounter. If you run up perfmon.exe, you'll see the range of performance counters available to you (set the 'performance object' to 'Process'), one of which is '% Processor Time'.
You can through the System.Diagnostic.PerformanceCounter class. Here's an example of somebody monitoring CPU usage:
http://blogs.msdn.com/dotnetinterop/archive/2007/02/02/system-diagnostics-performancecounter-and-processor-time-on-multi-core-or-multi-cpu.aspx
Note that this does require elevated privileges. And there may be a performance hit using it.
It is good that you are logging to monitors like smartinspect. But windows itself gathers the data for each resource in this case your program (or process). WMI is the standard for Application monitoring. We can view the data captured by WMI. Many application management, health monitoring or applicaiton monitoring tools support WMI out of the box.
So I would not recommend you to log your CPU usage within the application to a log file.
If you think availablity and performance is critical then go for solutions like Microsoft Operations manager solution.
To get an idea about WMI and to get the list of process see below:
- Win32_PerfFormattedData_PerfProc_Process to get Cpu time, filter is processID
See this article
- You can get the processID from Win32_process class.
WMI Made Easy For C# by Kevin Matthew Goss
oConn.Username = "JohnDoe";
oConn.Password = "JohnsPass";
System.Management.ManagementScope oMs = new System.Management.ManagementScope("\\MachineX", oConn);
//get Fixed disk stats
System.Management.ObjectQuery oQuery = new System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3");
//Execute the query
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oMs,oQuery);
//Get the results
ManagementObjectCollection oReturnCollection = oSearcher.Get();
//loop through found drives and write out info
foreach( ManagementObject oReturn in oReturnCollection )
{
// Disk name
Console.WriteLine("Name : " + oReturn["Name"].ToString());
// Free Space in bytes
Console.WriteLine("FreeSpace: " + oReturn["FreeSpace"].ToString());
// Size in bytes
Console.WriteLine("Size: " + oReturn["Size"].ToString());
}
You can monitor the process from Remote system as well.
This code project article describes how to use the high performance timer:
http://www.codeproject.com/KB/cs/highperformancetimercshar.aspx
You can use it to time the execution of your code.
Here you can find a number of open source C# profilers:
http://csharp-source.net/open-source/profile
Related
I created a little hardware lock in my licensing process. A (hopefully) unique hardware ID is generated before installation and encoded as part of my license file. Whenever my application starts, the hardware key is regenerated and compared to the registered one.
Actually, the hardware key refers to the first CPU, BIOS and mainboard infos based on the solution by Alex Sutu.
So far so good - now, the system runs in a virtual environment where new CPUs are linked in dynamically - and so the order of CPUs changes. with the invalidated key, the software is not running anymore - as wanted and expected by me, not by my client. Therefore I have to change the process a bit.
As far as I remember, Windows 7 (or even previous versions) had some kind of robustness within their activation / registration IDs - smaller hardware changes did not invalidate the activation / license - an that is what I want to mimic in my licensing context.
I found a little explaination here - which lets me think of the following process in general:
For registration:
Generate X different hardware IDs using different settings (CPU, mainboard, bios, whatever)
Register these IDs within the license file
When starting the application:
Generate X different hardware IDs using different settings (CPU, mainboard, bios, whatever) (is WMI access fast enough? Alternatives?)
If more than X percent (95?) of the the newly generated ones are equal to the registered ones from the license file, everything is fine and the application starts
Actually, generating a hardware ID using WMI queries takes pretty long (somewhat between 4 to 8 seconds).
static string identifier(string wmiClass, string wmProperty)
{
string result = "";
var mc = new ManagementClass(wmiClass);
var moc = mc.GetInstances();
foreach (var mo in moc)
{
if (result == "")
{
try
{
result = mo[wmProperty].ToString();
//Only get the first one
break;
}
catch
{
}
}
}
return result;
}
Example call:
string retVal = identifier("Win32_Processor", "UniqueId");
So I am quit a bit uncertain, whether taking more IDs into account is the right choice.
Therefore the final question:
Is this a practicable way of creating a robust hardware licensing or am I missing a point?
Secondly, if this is "state of the art", is my WMI approach the right one or are there better ways?
Thanks a lot in advance!
Your approach is generally correct. Collect different identifiers, store them securely and issue the license. Whenever only a few of the identifiers have changed, you update your storage to the new values, but still run the application.
As of the time to collect HW info, you may start loading the application in "locked mode". Once HW identifiers become available, you check the license and asynchronously unlock the application. This way you are not so limited by the communication delay - I used this approach with license validation against a remote server.
When I pass this below link query I find that the CPU usage goes till 100%
Query for searching the files
How Could I minimize this usage ,because if other application simultaneously runs it may cause harm to those application .I saw WMIPrvSE.exe is consuming CPU's 100 % memory.I can neither exit this application because it is fetching the data through WMI.
I do search other system queries from the WMI having different object for management object searcher as given in this link
Query for finding users.
When I completely execute the whole program ,I find it that CPU usage goes entirely up to 100% and doesn't come down till the application fetches all the data.
While debugging the code the usage goes down but trying to put Thread.Sleep doesn't do anything.
I need to get some of the values that are in the windows 7 resource monitor. In particular,I have found the memory usage and cpu but i am not able to get the bandwidth per process. I've looked into the PerformanceCounter class and I don't see a way to drill down to the process network level. The resource monitor has exactly what I am looking for. I looked into WMI as well and it seems most people recommend not using it but if anyone has WMI query as well then please let me know.
One most important thing that i want to tell you is that i don't want to get the calculated bandwidth of Network Interface , I want per process as shown in the resource monitor of the task manager.
So does anyone know how I can get this statistics at the process level using .net classes?
I am trying to perform parallel processing by lauching a console application program2 that does the work stuff. It is launched by program1 which knows how many instances to launch.
At some point the program can't launch more instances. Even if you increase the instancesmount, it only launches to a limit. In this case only 92. if I set the limit to 100 or 200 it still only launches 92 on the server.
I am writing the program in c# and it runs in windows server 2008.
Here is the code:
for (int instanceCount = 0; instanceCount < InstancesAmount; instanceCount++)
{
using (System.Diagnostics.Process myProcess = new System.Diagnostics.Process())
{
if (hiddeConsoleWindow)
{
myProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
}
myProcess.StartInfo.FileName = ExecutablePathProgram2;
System.Security.SecureString password = new System.Security.SecureString();
foreach (char c in ConfigurationSettingsManager.ProcessStartPassword.ToCharArray())
{
password.AppendChar(c);
}
myProcess.StartInfo.UserName = ConfigurationSettingsManager.ProcessStartUserName;
myProcess.StartInfo.Password = password;
myProcess.StartInfo.Domain = ConfigurationSettingsManager.ProcessStartDomain;
myProcess.StartInfo.UseShellExecute = false;
myProcess.Start();
}
}
I have been looking if there is max instances to launch but it always says that it is as many as the OS supports.
I also checked if there is a max instances per session or per user but couldn't find anything that describes something like that or I did miss it.
To quote Raymond Chen's blog: "If you have to ask about various operating system limits, you're probably doing something wrong".
There's a limit to how much work can actually get done by the computer even with that many processes. You will be better served by determining the number of processors in the system and choosing that many concurrent tasks to execute. Your "program1" could then launch the process and use the StartInfo to watch for when the process ends (and capture any error output in the meantime by redirecting the output and error streams and logging them as needed. Once a process completes, then you should launch the next one in the queue.
When you launch that many processes, the system will be thrashing trying to switch context between 100 processes and won't get much of anything done.
You may be running into memory limits depending on how much memory your child processes allocate. You'll have a bunch of processes starting up and taking up chunks of memory but doing nothing until their turn at the processor comes around. If it can't allocate the memory, it will probably choke and kill the process (depending on how error handling is done).
That is strange, as there is no hard limit by default. But of course, that depends on what is the launched process doing (memory consumption, handle allocation, files, etc.). Fo example, I have tested it with "notepad.exe" on my machine and I get 150 notepad.exe running if I specify 150 instances.
You can check here for a very interesting discussion on process limits: Pushing the Limits of Windows: Processes and Threads.
First, I definitely agree with #Garo Yeriazarian. But to be thorough I'd recommend checking out this blog post:
http://xentelworker.blogspot.com/2005/10/i-open-100-explorer-windows-and-my.html
I use System.Diagnostics.Process.GetProcesses() to get process list.
I found TotalProcessorTime property for each process - it is TimeSpan.
But how to get relative values of CPU usage, i. e. i need % of total CPU usage for each running process.
use WQL (queries WMI like SQL)
see attached link for few samples:WQL
Win32_PerfFormattedData_PerfProc_Process is your class for getting CPU data.
I found a workaround decision see this question
The plan is as follows: take counters data in stndard way, then process log files.
Realtime monitoring is not critical.