ProfileTest.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:5k
源码类别:

流媒体/Mpeg4/MP4

开发平台:

C/C++

  1. /* ====================================================================
  2.  * The Vovida Software License, Version 1.0 
  3.  * 
  4.  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
  5.  * 
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 
  13.  * 2. Redistributions in binary form must reproduce the above copyright
  14.  *    notice, this list of conditions and the following disclaimer in
  15.  *    the documentation and/or other materials provided with the
  16.  *    distribution.
  17.  * 
  18.  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
  19.  *    and "Vovida Open Communication Application Library (VOCAL)" must
  20.  *    not be used to endorse or promote products derived from this
  21.  *    software without prior written permission. For written
  22.  *    permission, please contact vocal@vovida.org.
  23.  *
  24.  * 4. Products derived from this software may not be called "VOCAL", nor
  25.  *    may "VOCAL" appear in their name, without prior written
  26.  *    permission of Vovida Networks, Inc.
  27.  * 
  28.  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
  29.  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30.  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
  31.  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
  32.  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
  33.  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
  34.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  35.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  36.  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
  37.  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  38.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
  39.  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  40.  * DAMAGE.
  41.  * 
  42.  * ====================================================================
  43.  * 
  44.  * This software consists of voluntary contributions made by Vovida
  45.  * Networks, Inc. and many individuals on behalf of Vovida Networks,
  46.  * Inc.  For more information on Vovida Networks, Inc., please see
  47.  * <http://www.vovida.org/>.
  48.  *
  49.  */
  50. static const char* const ProfileTest_cxx_Version =
  51.     "$Id: ProfileTest.cxx,v 1.7 2001/06/29 03:42:30 bko Exp $";
  52. #include <assert.h>
  53. #include "VThread.hxx"
  54. #include <unistd.h>
  55. #include <stdio.h>
  56. void profile0()
  57. {
  58.     for (int i = 0; i < 50; i++);
  59. }
  60. void profile1()
  61. {
  62.     for (int i = 0; i < 100; i++);
  63. }
  64. #define THREAD_PROFILE 1
  65. void* runner(void* proportion)
  66. {
  67.     int props = ((int) proportion) | 0x7f;
  68.     int runme = ((int) proportion) & 0x80;
  69. #if THREAD_PROFILE
  70.     u_short buf[1000000];
  71.     size_t addr = ((size_t) & profile0) - 100000;
  72.     if (runme)
  73.     {
  74.         memset(buf, 0, 1000000);
  75.         profil(buf, 1000000, addr, 65536);
  76.     }
  77. #endif
  78.     int count0 = 0;
  79.     int count1 = 0;
  80.     for (int i = 0; i < 1000000; i++)
  81.     {
  82.         if (i % props)
  83.         {
  84.             profile0();
  85.             count0++;
  86.         }
  87.         else
  88.         {
  89.             profile1();
  90.             count1++;
  91.         }
  92.     }
  93. #if THREAD_PROFILE
  94.     if (runme)
  95.     {
  96.         printf ("%dn", addr);
  97.         profil(0, 1000000, addr, 65536);
  98.         for (int j = 0; j < 1000000; j++)
  99.         {
  100.             if (buf[j] > 0)
  101.             {
  102.                 printf ("%d: %d: %dn",
  103.                         VThread::selfId(),
  104.                         addr + j, buf[j]);
  105.             }
  106.         }
  107.     }
  108. #endif
  109.     return 0;
  110. }
  111. int main()
  112. {
  113. #if 1
  114. #if !THREAD_PROFILE
  115.     size_t addr = ((size_t) & profile0) - 100000;
  116.     u_short buf[1000000];
  117.     memset(buf, 0, 1000000);
  118.     profil(buf, 1000000, addr, 65536);
  119. #endif
  120.     VThread thread1;
  121.     VThread thread2;
  122.     VThread thread3;
  123.     VThread thread4;
  124.     VThread thread5;
  125.     thread1.spawn(&runner, (void*) (2 | 0x80));
  126.     thread2.spawn(&runner, (void*) 2);
  127.     thread3.spawn(&runner, (void*) 2);
  128.     thread4.spawn(&runner, (void*) 2);
  129.     thread5.spawn(&runner, (void*) 2);
  130.     thread1.join();
  131.     thread2.join();
  132.     thread3.join();
  133.     thread4.join();
  134.     thread5.join();
  135. #if !THREAD_PROFILE
  136.     profil(0, 1000000, addr, 65536);
  137.     for (int j = 0; j < 1000000; j++)
  138.     {
  139.         if (buf[j] > 0)
  140.         {
  141.             printf ("%d: %d: %dn",
  142.                     VThread::selfId(),
  143.                     addr + j, buf[j]);
  144.         }
  145.     }
  146. #endif
  147. #else
  148. runner((void*) 2);
  149. #endif
  150.     return 0;
  151. }