Modify Trace File on ns-2

to reduce your trace file size or clear what you want to to see or analyse trace file easily first of all you should learn how to rerad trace file then you can modify your trace file as follows.

Modified folders: ns-2/trace/, cmu-trace.h

1.Add new format type in cmu-trace.h.
class CMUTrace : public Trace {

void format_x(Packet *p, int offset);


2. Defined new format type in

void CMUTrace::format_x(Packet *p, int offset)
struct hdr_ip *ih = HDR_IP(p);
hdr_iett *hdr = hdr_iett::access(p);
double x = 0.0, y = 0.0, z = 0.0;
char op = (char) type_;
node_->getLoc(&x, &y, &z);
FILE * pFile;
int src = Address::instance().get_nodeaddr(ih->saddr());
pFile = fopen (“”,”a”);//if you want to have net values you should save the current .tr and .nam //thats why you should geberate new file to save your trace file lines…

if (op == ‘r’ && src!=src_)
switch (hdr->x_pt)

case xxxxx:

fprintf (pFile, “%.9f %d %d %s\n”,Scheduler::instance().clock(), // packet reception time
src, // packet src
src_, // packet receiver
x_packet_type_names[hdr->x_pt] //packet type

default : {;}

fclose (pFile);

If you get an error “Segmentation fault” pls check your file read and write operation…

If you get “Floating point exception” pls check your numerical values. Maybe you missed somthing in your rpogram.
For example; may be you are trying to divide your number to the ZERO
may be you are trying to generate a random index from your array even it is not get any value in it.

48 thoughts on “Modify Trace File on ns-2

  1. rami August 27, 2009 at 9:19 am Reply


    you used the type hdr_iett and i didn’t find this type anywhere.. could you please explain ….


    • hilalkaratoy September 4, 2009 at 8:18 pm Reply

      hdr_iett is just a packet name… you can’t find because we generated it…It means you can generate new type of packet pr packet header..

  2. reza October 25, 2009 at 2:13 pm Reply

    dear sir i try to using emulation facility in NS-2,
    but i got error “Floating point exception” .Based from your blog,
    Can you explain to me where i’ve to check numerical values, in my tcl simulation script or the others folder in ns2?
    i’m using ns2 version 2.33 and i try to implements greedy perimeter stateless routing (gpsr) .Thanks for your attention.

    • hilalkaratoy October 28, 2009 at 8:47 am Reply

      Floating point exception means you are trying to devide something to zero… but it is not exact answer just such an example of getting that error message.
      In ns-2 there is no any (my observation) division problem just you chech your calculation for example maybe you are trying to find a number in your array even if your array is empty this is just an example i couldnt know what is the exact answer but YOU SHOULD CHECHK YOUR CALCULATION debug your code…

  3. Gaurav March 8, 2010 at 9:55 am Reply

    Hello Sir,

    I don’t want the new format. I just want to do some changes in the current format. Thats it.
    So, how could it be done?

  4. M Ejaz March 11, 2010 at 2:15 pm Reply

    dear sir, i have one entry in packet cmn header that is channelindex_, i want to display that value in trace file too. how can i display that?

    • yas November 18, 2010 at 7:43 am Reply

      Do you find how to display the modified value in trace file ?

      • hilalkaratoy November 22, 2010 at 8:32 am

        just you will print the value which you changed….if this value is used then it will be available to print also. just call that value where you want to use….

  5. J Lart April 15, 2010 at 1:15 am Reply

    Hi There,

    I have ns-2.29 installed on my pc i want to analyse energy parameters on the aodv and dsr routing protocols using awk scripts.
    My tcl file is giving me errors anytime i run them,have you pls got any tcl files that incorporates energy parameters and also how to go abt the analysis. do you think i need to upgrade the version of my ns-2 as i think most of my errors are compatability issues as im using vista.
    Thanks and ope to hear frm u soon.

  6. umang June 4, 2010 at 4:14 pm Reply

    I am facing of problem of floating point exception in modified file.please let me know how to handle it.

    • hilalkaratoy June 6, 2010 at 8:58 pm Reply

      I also faced that problem… I found solution as checking my calculations and I realized that my array doesnt have any value and I my calculation try to use for division. thats why you should check your calculation. maybe you try to divide some number to zero or something like that.
      because ns-2 doesnt have any problem about calculation…
      (at least thats was my problem because this error mesaage like error no meaning )

  7. samar July 5, 2010 at 9:21 am Reply

    i am using ns 2.29
    and i want to know the trace file format and how to read it to extract information from it using awk
    s 0.505582352 _5_ RTR — 0 AODV 237 [0 ffffffff 2 800] ——- [5:255 -1:255 29 0] [0x2 2 1 [4 0] [2 4]] (REQUEST)
    s 0.506864067 _3_ RTR — 0 AODV 237 [0 ffffffff 2 800] ——- [3:255 -1:255 29 0] [0x2 2 1 [4 0] [2 4]] (REQUEST)
    r 0.508346725 _2_ RTR — 0 AODV 196 [13a 2 4 800] ——- [4:255 2:255 30 2] [0x4 1 [4 4] 10.000000] (REPLY)
    s 0.508346725 _2_ RTR — 0 cbr 520 [0 0 0 0] ——- [2:0 4:0 30 4] [0] 0 0
    s 0.510000000 _2_ AGT — 2 cbr 500 [0 0 0 0] ——- [2:0 4:0 32 0] [2] 0 0
    r 0.510000000 _2_ RTR — 2 cbr 500 [0 0 0 0] ——- [2:0 4:0 32 0] [2] 0 0
    s 0.510000000 _2_ RTR — 2 cbr 520 [0 0 0 0] ——- [2:0 4:0 30 4] [2] 0 0
    what each field means.
    please reply me as soon as possible.

    • hilalkaratoy July 5, 2010 at 10:10 pm Reply
      look at this site
      in here ns-2 document exist and you can read your new trace file
      if you want to repharase your tarce file then read my trace file editing post….
      this side will help you and trace file also trace your node when it is sending data even from MAC

      • ram December 29, 2011 at 1:34 pm

        hello samar……did you get any thing in form me

      • adil December 10, 2012 at 7:18 pm

        hello, i am new on ns2. can u tell me plz how to turn on energy trace in tcl file and how to open that energy trace file.

      • hilalkaratoy December 11, 2012 at 11:18 pm

        Energy should be shown on the current trace file. U need to read ns2 manual … There a section there explain this…

  8. shilpa December 7, 2010 at 5:35 pm Reply

    hello sir.
    i want to calculate energy consumed by the nodes……so i m in need of awk file for energy..pls do the needful……..

  9. shilpa December 7, 2010 at 5:36 pm Reply

    hello sir.
    i m need of awk file for energy consumption by the nodes.pls do the needful..

  10. mahsa February 24, 2011 at 4:33 am Reply

    if i want to send my own traffic ,I mean NS2 read the content of a file and send them ,like a log if video ,Do you know what should I do ??

  11. mahsa February 24, 2011 at 4:34 am Reply

    if i want to send my own traffic ,I mean NS2 read the content of a file and send them ,like a log if video ,Do you know what should I do ??

  12. prapulla February 24, 2011 at 5:51 pm Reply

    hello sir……………….
    i am calculating throughput and delay for AODV tcl script……………but am unable to verify whether the generated output is correct or not……………….pls help me

  13. A friend - Question June 7, 2011 at 2:41 am Reply

    hi my dear friend
    i have many question in ns.

    1) how i can simulate a webserver (how code or command or class & objects?)

    2) how i can install gnuplot?
    - how i can show the output analyze file, in gnuplot? (how code or command?)

    3) if i use SCTP Protocol&Agent,how should i do? (how code or command?)

    very thanks…

  14. prabin January 26, 2012 at 5:32 am Reply

    Hi i’m new user to ns2 and i want to piggyback or combine two packets at a time and then send those packets in the multicast networks. Is it possible by ns2?

  15. Sayan February 29, 2012 at 7:17 pm Reply

    Dear Sir
    I wanted to know how can I run a ns2 project if I have common, mac, tcl, tcl and trace folders in a project with their respectictive codes in a certain project.
    Thank you.

    • hilalkaratoy April 21, 2012 at 10:58 pm Reply

      Under ns2 title it is written with all the details!!

  16. evangeline March 11, 2012 at 7:25 am Reply

    hi sir
    i too need awk file for energy consumption for AODV and me how to view enerrgy information in old trace format..

  17. mukesh kumar March 28, 2012 at 5:08 am Reply

    sir i want to change my trace file in this format “s -t 1.000000000 -Hs 0 -Hd -2 -Ni 0 -Nx 252.64 -Ny 235.76 -Nz 0.00 -Ne -1.000000 -Nl AGT -Nw — -Ma 0 -Md 0 -Ms 0
    -Mt 0 -Is 0.0 -Id 1.0 -It cbr -Il 512 -If 0 -Ii 0 -Iv 32 -Pn cbr -Pi 0 -Pf 0 -Po 4″ How could it possible where i need to change please explain me ……….

  18. Najma Ismat April 23, 2012 at 9:01 am Reply

    i’ve simulated a flat wsn of 5 nodes with mobility ( mobility pattern is generated using bonnmotion tool) and sink is placed in the middle of the sensing field of 100x100m. the trace output shows that all the application level packets get dropped and 0 packets received at the sink. the reason of packet drop is either ARP or NRTE, trace output also shows the exact movement of sensor nodes as provided. please guide whats wrong with the simulation that sink is not receiving any data.

  19. ns2-lina November 1, 2012 at 10:54 am Reply

    I used NS2.34 in linux, I work with a ring topology traffic multicast. In the tcl code I used join/leave, in the trace file there are prune.
    The problem is that I can not remove the prune in the trace file. Please help me if possible to remove the prune.
    Thank you for your help

    • hilalkaratoy November 4, 2012 at 10:20 am Reply

      In order to have custom trace file there are some setting change or commands. I dont remeber exactly whic one the solution but you can change the trrace file library and rebuild it again. Please read ns-2 document Tracing section

    • hilalkaratoy November 4, 2012 at 10:21 am Reply

      Or you can write another script to cleanup the created trace file from prune

  20. Gaurish Edake January 14, 2013 at 9:31 am Reply

    I want to utilize energy trace from old trace format of sensor node, but unable to get it.
    So will you pls provide me some guidance regarding the same.

    • hilalkaratoy January 17, 2013 at 9:12 pm Reply

      Please read the trace files format for the old version in ns2 pdf… U need to modify ur code wrt that format…

  21. Dalia January 14, 2013 at 1:34 pm Reply

    hey hilal ,,hope u r fine :)..
    i tried to modify ns2 energy model , i added life time function, that calculate life time of specific node at specific time ..then i tried to print lifetime value in my trace file (in case of using energy model).. to get the following output in my trace file ..” [energy ei es et er lifetime]”
    1- in file (line 345) ,i changed sprintf command line to be as the following ”
    sprintf(pt_->buffer() + offset,
    “[energy %f ei %.3f es %.3f et %.3f er %.3f lifetime %f] “,
    2- i recompiled ns2 using make command and i did’t get any errors..

    MY PROBLEM IS : when i simulate a simple script using energy model ..lifetime doesn’t appear in my trace file ..& i don’t know the reason of this .. do i have to change something else for example? ..
    hope that u can help me in this situation..

    Dalia Elewely

    • hilalkaratoy January 17, 2013 at 9:14 pm Reply

      Hi, if u read ns2 pdf it should tell what u need to do, also u can read the post related with the trace file. I do not have comp currently for this reason i couldnt provide the elaborated answer :(

  22. Ajay February 26, 2013 at 5:05 am Reply

    Hi mam,
    I’m working on aodv routing protocol. Initially, in network deployment phase every node should broadcast packets for a certain period of time. This should be done before aodv routing protocol starts. Mam how can i get all broadcasting details in trace file. Mam i need your help. Waiting for your replay.


    • hilalkaratoy February 26, 2013 at 11:24 pm Reply

      When u do tracing all the data are automaticlly stored in the trace file.,, of course after u run nam….
      As an advice please try extremely simple
      Examples before u dive into…. Enty of examples in ns2 example folder….

  23. Dashti March 3, 2013 at 11:51 pm Reply

    hello hilalkaratoy,
    I need your help, I’m trying to evaluate energy for DSR,DSDV and AODV respectively. I’m having problem with DSDV, when I play in nam I can not the traffic flow between senders and destination therefore in the trace file there are alot of drop packets. do you have any idea what is wrong?
    FYI: using same scenario for others was fine


  24. adarksun April 5, 2013 at 6:25 am Reply

    I’m learning ns2 and I got the error “floating point exception” when I am trying to simulate
    ” simple-wireless.tcl” which is an example located into [ns2.34/tcl/ex folder] and dont change anything of that file.

    Would you plz help me, what type of issues may cause this problem.


    • hilalkaratoy April 5, 2013 at 12:55 pm Reply

      Do u provide any input? Else print the internal parameter and see which parameter causes this error, also u can pick up another example or simplfy the current one…
      I run the example tcl files they were fine…

  25. Najma April 28, 2013 at 8:57 am Reply

    Hi, I m simulating leach protocol to find the movement of node’s if any using getvel() function. I m getting floating point exception error during tcl script execution. I’ve not used any function except the methods available on the web for finding node’s position, energy and velocity during simulation. Please guide how to resolve this issue?

  26. zaineb May 24, 2013 at 4:39 pm Reply

    I going to add 1 octet to my paquet (size=512) so means that in this ligne : $cbr set packetSize_ 512 : add 8 to 512 or what ?????

  27. Nimat Ullah Khattak October 14, 2013 at 3:34 am Reply

    Hi, I’m simulating leach protocol, it shows more packets received in trace file than sent, where can be the problem? and how to correct it?

    • hilalkaratoy October 14, 2013 at 6:02 am Reply

      There could be
      loop somewhere, or
      time you set could be more than you think, or
      there is another node communicating with it or
      you have mistaken to read the trace file

  28. Nimat Ullah Khattak October 15, 2013 at 6:52 am Reply

    Thanks for your repy…
    Where can i turn on/off the mac and phy packets specifically ?

  29. bekka October 23, 2013 at 9:26 pm Reply

    i have pb with after exec awk i have ampty file before i have message awk can’t open

  30. kpk December 30, 2013 at 4:23 am Reply

    Thanks for your i can extract data from leach trace file.kindly help me.

  31. kpk December 30, 2013 at 6:51 am Reply

    how we can use mannasim with leach?kindly reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 35 other followers

%d bloggers like this: