timer in milliseconds

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • S_at_work

    timer in milliseconds


    hello,



    can anybody give me a piece of code , or a hint , how i can recieve the
    time i need for connect() to another host in milliseconds.



    My problem is i want test the performance of ipsec, so i need the
    time in milliseconds, how long it takes to connect to another host
    over ipsec.



    1) i tried clock() but clock() don't work, because connect() don't use
    cpu time like sleep(), so i always get 0 milliseconds, but the
    connect takes about 2 seconds (i test this with a sniffer) .



    is the any way to get the time in milliseconds ????



    thanks a lot and best regards, stefan


    --
    Posted via http://dbforums.com
  • Dan Pop

    #2
    Re: timer in milliseconds

    In <3526954.106726 8750@dbforums.c om> S_at_work <member43174@db forums.com> writes:

    [color=blue]
    >can anybody give me a piece of code , or a hint , how i can recieve the
    >time i need for connect() to another host in milliseconds.[/color]

    If you post this question to a newsgroup dedicated to programming on
    your platform, I'm reasonably sure someone can. There is no portable
    solution to your problem, the best you can get from standard C for
    measuring real time intervals is time() and it typically works with
    second resolution, i.e. three orders of magnitude worse than what you
    need.
    [color=blue]
    >1) i tried clock() but clock() don't work, because connect() don't use
    > cpu time like sleep(), so i always get 0 milliseconds, but the[/color]

    Where did you get the idea that sleep() uses CPU time from?
    [color=blue]
    > connect takes about 2 seconds (i test this with a sniffer) .[/color]

    Yup, forget about clock(), it's not what you need, regardless of any
    resolution issues: it does NOT measure real time intervals.
    [color=blue]
    >is the any way to get the time in milliseconds ????[/color]

    If you're on a Unix platform and if microseconds are good enough, check
    gettimeofday(2) .

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email: Dan.Pop@ifh.de

    Comment

    • Glen Herrmannsfeldt

      #3
      Re: timer in milliseconds


      "S_at_work" <member43174@db forums.com> wrote in message
      news:3526954.10 67268750@dbforu ms.com...
      [color=blue]
      > can anybody give me a piece of code , or a hint , how i can recieve the
      > time i need for connect() to another host in milliseconds.[/color]

      Many processors now have a register that counts clock cycles. If yours
      does, this probably will do what you want. (You also need to know the clock
      rate, though.)

      It takes a two instruction assembly program on x86 (off topic) , for
      example.

      -- glen


      Comment

      • Keith Thompson

        #4
        Re: timer in milliseconds

        Dan.Pop@cern.ch (Dan Pop) writes:
        [color=blue]
        > In <3526954.106726 8750@dbforums.c om> S_at_work
        > <member43174@db forums.com> writes:[/color]
        [...][color=blue][color=green]
        > >1) i tried clock() but clock() don't work, because connect() don't use
        > > cpu time like sleep(), so i always get 0 milliseconds, but the[/color]
        >
        > Where did you get the idea that sleep() uses CPU time from?[/color]

        I think he meant that connect(), like sleep(), doesn't use CPU time.
        (It uses some, of course, but not enough to make it useful to measure
        it.)

        --
        Keith Thompson (The_Other_Keit h) kst@cts.com <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
        Schroedinger does Shakespeare: "To be *and* not to be"

        Comment

        • Randy Howard

          #5
          Re: timer in milliseconds

          In article <shenb.42505$Tr 4.87514@attbi_s 03>, gah@ugcs.caltec h.edu says...[color=blue]
          > Many processors now have a register that counts clock cycles. If yours
          > does, this probably will do what you want. (You also need to know the clock
          > rate, though.)
          >
          > It takes a two instruction assembly program on x86 (off topic) , for
          > example.[/color]

          Please don't post this OT stuff, especially since it is likely to result
          in bad results. For example, the solution using rdtsc for x86 is horribly
          broken on SMP systems.

          --
          Randy Howard _o
          2reply remove FOOBAR \<,
          _______________ _______()/ ()_____________ _______________ _______________ ___
          SCO Spam-magnet: postmaster@sco. com

          Comment

          • Glen Herrmannsfeldt

            #6
            Re: timer in milliseconds


            "Randy Howard" <randy.howard@F OOmegapathdslBA R.net> wrote in message
            news:MPG.1a079c 3738070cf498992 3@news.megapath dsl.net...[color=blue]
            > In article <shenb.42505$Tr 4.87514@attbi_s 03>, gah@ugcs.caltec h.edu says...[color=green]
            > > Many processors now have a register that counts clock cycles. If yours
            > > does, this probably will do what you want. (You also need to know the[/color][/color]
            clock[color=blue][color=green]
            > > rate, though.)
            > >
            > > It takes a two instruction assembly program on x86 (off topic) , for
            > > example.[/color]
            >
            > Please don't post this OT stuff, especially since it is likely to result
            > in bad results. For example, the solution using rdtsc for x86 is horribly
            > broken on SMP systems.[/color]

            Certainly it us up to the user to understand the system in use.

            I have used rdtsc on a SMP system, without considering the problems that it
            might cause. As well as I remember it, it worked fine, but I can see that
            it could cause problems. I think the results will still be better than
            clock(), though.

            -- glen


            Comment

            • S_at_work

              #7
              Re: timer in milliseconds


              thanks for help now it works

              here is the code







              #include <stdio.h>

              #include <time.h>



              #define MAIN

              //#define DEBUG



              float GetTimeDelta(st ruct timeval *time1){





              float timereturn;

              struct timeval time2;

              time2.tv_usec=0 ;

              time2.tv_sec=0;





              if(time1->tv_usec==NULL& &time1->tv_sec==NULL ){

              timereturn = 0.0;

              gettimeofday(ti me1,NULL);

              }

              else{

              gettimeofday(&t ime2,NULL);

              if(time1->tv_usec > time2.tv_usec){

              timereturn =((1000000.0+(t ime2.tv_usec-time1-[color=blue]
              >tv_usec))/1000000.0);[/color]

              time2.tv_sec-=1;

              // printf(" milliseconds 1>2:%f\n",timer eturn);

              }

              else{

              timereturn =(time2.tv_usec-time1->tv_usec)/1000000.0;

              //printf(" milliseconds 2>1:%f\n",timer eturn);

              }

              timereturn+=(ti me2.tv_sec -time1->tv_sec);



              }

              #ifdef DEBUG

              printf("time1 seconds:%d\n",t ime1->tv_sec);

              printf("time1 milliseconds:%d \n",time1->tv_usec);



              printf("time2 seconds:%d\n",t ime2.tv_sec);

              printf("time2 milliseconds:%d \n",time2.tv_us ec);



              return timereturn;

              #endif DEBUG

              }





              #ifdef MAIN

              int main()

              {

              struct timeval time;

              time.tv_sec =0;

              time.tv_usec=0;

              int i=100000;

              int a=0;

              printf("start time %f\n",GetTimeDe lta(&time));



              //time consuming

              sleep(4);

              while(i--)

              a= i%12;

              //end time consuming



              printf("end time %f\n",GetTimeDe lta(&time));





              }

              #endif


              --
              Posted via http://dbforums.com

              Comment

              • Dan Pop

                #8
                Re: timer in milliseconds

                In <2Unnb.46121$Tr 4.103510@attbi_ s03> "Glen Herrmannsfeldt" <gah@ugcs.calte ch.edu> writes:

                [color=blue]
                >"Randy Howard" <randy.howard@F OOmegapathdslBA R.net> wrote in message
                >news:MPG.1a079 c3738070cf49899 23@news.megapat hdsl.net...[color=green]
                >> In article <shenb.42505$Tr 4.87514@attbi_s 03>, gah@ugcs.caltec h.edu says...[color=darkred]
                >> > Many processors now have a register that counts clock cycles. If yours
                >> > does, this probably will do what you want. (You also need to know the[/color][/color]
                >clock[color=green][color=darkred]
                >> > rate, though.)
                >> >
                >> > It takes a two instruction assembly program on x86 (off topic) , for
                >> > example.[/color]
                >>
                >> Please don't post this OT stuff, especially since it is likely to result
                >> in bad results. For example, the solution using rdtsc for x86 is horribly
                >> broken on SMP systems.[/color]
                >
                >Certainly it us up to the user to understand the system in use.
                >
                >I have used rdtsc on a SMP system, without considering the problems that it
                >might cause. As well as I remember it, it worked fine, but I can see that
                >it could cause problems. I think the results will still be better than
                >clock(), though.[/color]

                If the solution has to be platform-specific, anyway, why not use a *clean*
                solution provided by the implementation, as an extension? Especially
                since the OP only needs millisecond resolution.

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • Dan Pop

                  #9
                  Re: timer in milliseconds

                  In <3530524.106733 3452@dbforums.c om> S_at_work <member43174@db forums.com> writes:

                  [color=blue]
                  >thanks for help now it works
                  >
                  >here is the code[/color]

                  Why did you feel compelled to post non-portable C code to this newsgroup?

                  BTW, the *correct* usage of gettimeofday requires the inclusion of
                  <sys/time.h>.

                  Dan
                  --
                  Dan Pop
                  DESY Zeuthen, RZ group
                  Email: Dan.Pop@ifh.de

                  Comment

                  • Randy Howard

                    #10
                    OT: Re: timer in milliseconds

                    In article <2Unnb.46121$Tr 4.103510@attbi_ s03>, gah@ugcs.caltec h.edu
                    says...[color=blue]
                    > Certainly it us up to the user to understand the system in use.[/color]

                    Absolutely.
                    [color=blue]
                    > I have used rdtsc on a SMP system, without considering the problems that it
                    > might cause. As well as I remember it, it worked fine, but I can see that
                    > it could cause problems.[/color]

                    The problem is that the tsc data is not synched between CPUs, meaning that
                    your results are only accurate as long as your process (or thread) is
                    pinned to a specific CPU. That's usually not the case (nor ideal).


                    --
                    Randy Howard _o
                    2reply remove FOOBAR \<,
                    _______________ _______()/ ()_____________ _______________ _______________ ___
                    SCO Spam-magnet: postmaster@sco. com

                    Comment

                    • Glen Herrmannsfeldt

                      #11
                      Re: Re: timer in milliseconds


                      "Randy Howard" <randy.howard@F OOmegapathdslBA R.net> wrote in message
                      news:MPG.1a0849 916c8be11a98992 7@news.megapath dsl.net...[color=blue]
                      > In article <2Unnb.46121$Tr 4.103510@attbi_ s03>, gah@ugcs.caltec h.edu
                      > says...[color=green]
                      > > Certainly it us up to the user to understand the system in use.[/color]
                      >
                      > Absolutely.
                      >[color=green]
                      > > I have used rdtsc on a SMP system, without considering the problems that[/color][/color]
                      it[color=blue][color=green]
                      > > might cause. As well as I remember it, it worked fine, but I can see[/color][/color]
                      that[color=blue][color=green]
                      > > it could cause problems.[/color]
                      >
                      > The problem is that the tsc data is not synched between CPUs, meaning that
                      > your results are only accurate as long as your process (or thread) is
                      > pinned to a specific CPU. That's usually not the case (nor ideal).[/color]

                      I have a post to comp.sys.intel to see if anyone there knows. I know TSC
                      is zero at reset, and if all leave reset at the same time, they may be
                      synchronized. I will see if anyone answers there.

                      The one time I did it on a four way SMP machine was in Java, using a JVM
                      native method, which means C. I took a simple C program, compiled it
                      with -S, modified the result with an RDTSC instruction, assembled it, and it
                      pretty much worked. I didn't need nanosecond resolution, but millisecond
                      may not have been good enough. There are enough problems with the way Java
                      native method calls not to worry too much about the SMP problem.

                      I will see what they say on comp.sys.intel, though. I have a dual 350MHz P2
                      system at home, but I never tried it on that one.

                      -- glen


                      Comment

                      • Randy Howard

                        #12
                        Re: Re: timer in milliseconds

                        In article <hyAnb.51086$Tr 4.105803@attbi_ s03>, gah@ugcs.caltec h.edu
                        says...[color=blue][color=green]
                        > > The problem is that the tsc data is not synched between CPUs, meaning that
                        > > your results are only accurate as long as your process (or thread) is
                        > > pinned to a specific CPU. That's usually not the case (nor ideal).[/color]
                        >
                        > I have a post to comp.sys.intel to see if anyone there knows. I know TSC
                        > is zero at reset, and if all leave reset at the same time, they may be
                        > synchronized. I will see if anyone answers there.[/color]

                        Been there done that. The folks I asked at Intel admit that is the case.
                        It's probably documented as such formally. I believe it was discussed
                        recently on the threading forum at Intel's website and verified as
                        unreliable" by Intel employees there. It has certainly been discussed in
                        plenty of places. Please let me know if you get a different answer, but I
                        have seen it give erratic results (I.e. negative tsc differences inside of
                        wraparound times) due to TSC skew between processors. You may need to
                        have a lot of dynamic load on the box such that the scheduler is moving
                        processes around between CPUs before it becomes obvious.

                        --
                        Randy Howard _o
                        2reply remove FOOBAR \<,
                        _______________ _______()/ ()_____________ _______________ _______________ ___
                        SCO Spam-magnet: postmaster@sco. com

                        Comment

                        Working...