Solved

Bandwidth usage given a process id

Posted on 2004-08-14
12
831 Views
Last Modified: 2008-02-01
Given a process id I can get reference to Process object by

int processID = 0;
//initialize processID from whatever
System.Diagnostics.Process process =
  System.Diagnostics.Process.GetProcessById(processID);

Now what I'd like to do is figure out how much bandwidth that particular process is using up.  I know what ports in particular it is using but it would also be nice to know how to figure out what ports a process is using and there current state.

At any rate my main question how do I determine how many bytes, packets, etc that this process is either downloading or uploading?

Code is much appreciated as I just switched over to c# from c++.

Thanks
0
Comment
Question by:PerryDK
  • 4
  • 3
  • 2
  • +1
12 Comments
 
LVL 5

Expert Comment

by:knowlton
ID: 11806309
You are talking about monitoring an FTP session while it is downloading something, fo example?
0
 
LVL 5

Expert Comment

by:knowlton
ID: 11806318
One thing you might try:

1)  Create your own VERY simple FTP client in C#.
2)  Hook into the process id (in Task Manager) while the FTP client is at rest.
3)  Make note of the amount of RAM used by the process while at rest.
4)  Start a download of a file who's size you know in advance.
5)  Record the additional RAM consumption and see if it adds up.

I am at a loss as to how you would monitor the bandwidth......very interesting problem!
0
 
LVL 4

Author Comment

by:PerryDK
ID: 11806905
No I'm not talking about FTP.

Lets say:
Internet BackGammon is running with pid 1
Some FTP Client with pid 2
Instant Messenger with pid 3
...

I can get the protocol, ports, pid associated with a given process by using
C:\Documents and Settings>netstat -n -o

Active Connections

  Proto  Local Address          Foreign Address        State           PID
  TCP    192.168.0.101:1248     192.168.0.1:5678       CLOSE_WAIT      696
  TCP    192.168.0.101:1700     192.168.0.1:5678       TIME_WAIT       0
  TCP    192.168.0.101:1701     192.168.0.1:5678       TIME_WAIT       0
 ...
  TCP    192.168.0.101:1800     192.168.0.1:5678       TIME_WAIT       0
  TCP    192.168.0.101:1801     192.168.0.1:5678       TIME_WAIT       0
  TCP    192.168.0.101:4927     207.46.106.140:1863    ESTABLISHED     852


I can also get overall statistics such as
C:\Documents and Settings>netstat -e
Interface Statistics

                           Received            Sent

Bytes                     220854897       158609049
Unicast packets              792485          943036
Non-unicast packets           85678            1832
Discards                          0               0
Errors                            0               0
Unknown protocols             10245

Now the problem lies in that the EthernetStatistics I get from using the netstat -e command are overall statistics.  What I'd like to do is either through some command line utility (must come with windows or is freeware) or by doing it programmitically in C# is:

C:\Documents and Settings>netstat -pid=1
Internet BackGammon Statistics
                           Received            Sent
Bytes                     220854897       158609049

Note the above does not work its just an example of the output I would like to create.

In otherwords I want to monitor the bandwidth usage of a given process.

I hope that helps explains things further.
0
 
LVL 4

Author Comment

by:PerryDK
ID: 11806967
Also would be nice to know how to get same info from netstat -e in C#

Currently I am executing netstat -e and redirecting the standard output...although there is probably a function call or something similar I could be using.
0
 
LVL 4

Author Comment

by:PerryDK
ID: 11817530
Never mind on last comment figured out how with PerformanceCounters...but would still very much like to know how to track the information down to a given process.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

by:ErikPhilips
ID: 11817840
You should see if IIS has its own performance counters.  I'm thinking you are looking at OS performance counters.  I can't imagine that IIS doesn't have its own performance counters.  (if you don't see them by default you'll most likely have to turn them on, I would imagine they are off by default because they use system resources, why waste that information if no one is watching :).
0
 
LVL 4

Expert Comment

by:ErikPhilips
ID: 11817856
Whoops I missed your comment "No I'm not talking about FTP."
0
 
LVL 4

Author Comment

by:PerryDK
ID: 11817872
You'll have to forgive me...I am very new to C# (4 days old)...I don't know what IIS is?  

Seeing that your last statment says:
Whoops I missed your comment "No I'm not talking about FTP."

I assume IIS has nothing to do with the question...if I am mistaken please inform.
0
 
LVL 4

Accepted Solution

by:
ErikPhilips earned 250 total points
ID: 11817880
Microsoft Internet Information Server (IIS web/ftp server) with counters you could read.  You could read how much data in/out of the FTP server.  But then I realized thats not what you wanted.
0
 
LVL 4

Assisted Solution

by:Eran_R
Eran_R earned 250 total points
ID: 11873622
Take a look at the crazy technology called "Winsock 2 Layered Service Providers" that enable writing one to gather bandwidth data per process.

http://www.microsoft.com/msj/0599/LayeredService/LayeredService.aspx

It's quite difficult to master, but you could write an LSP (Layered Service Provider) that adds metering for Socket I/O on a per-process basis
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now