~ubuntu-branches/ubuntu/quantal/open-vm-tools/quantal-201210021442

« back to all changes in this revision

Viewing changes to lib/lock/ulSema.c

  • Committer: Package Import Robot
  • Author(s): Nate Muench
  • Date: 2012-06-20 15:59:51 UTC
  • mfrom: (1.4.8)
  • Revision ID: package-import@ubuntu.com-20120620155951-6rupmpb0f70b52zr
Tags: 2012.05.21-724730-0ubuntu1
* Merging upstream version 2012.05.21-724730.
  - Fixes building against the current Quantal kernel. (LP: #1000344)
  - Fixes Quantal installation issues. (LP: #1019031)

* Sync with Debian
  - Updating to debhelper version 9.
  - Updating to standards version 3.9.3.
  - Updating copyright file machine-readable format version 1.0.
  - Building without multiarch paths for now

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
 
58
58
typedef struct
59
59
{
60
 
   MXUserAcquisitionStats  acquisitionStats;
61
 
   Atomic_Ptr              acquisitionHisto;
62
 
} MXUserStats;
 
60
   MXUserAcquisitionStats  data;
 
61
   Atomic_Ptr              histo;
 
62
} MXUserAcquireStats;
63
63
 
64
64
struct MXUserSemaphore
65
65
{
66
66
   MXUserHeader     header;
67
67
   Atomic_uint32    activeUserCount;
68
68
   NativeSemaphore  nativeSemaphore;
69
 
   Atomic_Ptr       statsMem;
 
69
   Atomic_Ptr       acquireStatsMem;
70
70
};
71
71
 
72
72
 
403
403
MXUserStatsActionSema(MXUserHeader *header)  // IN:
404
404
{
405
405
   MXUserSemaphore *sema = (MXUserSemaphore *) header;
406
 
   MXUserStats *stats = (MXUserStats *) Atomic_ReadPtr(&sema->statsMem);
 
406
   MXUserAcquireStats *acquireStats = Atomic_ReadPtr(&sema->acquireStatsMem);
407
407
 
408
 
   if (stats) {
 
408
   if (LIKELY(acquireStats != NULL)) {
409
409
      Bool isHot;
410
410
      Bool doLog;
411
411
      double contentionRatio;
414
414
       * Dump the statistics for the specified semaphore.
415
415
       */
416
416
 
417
 
      MXUserDumpAcquisitionStats(&stats->acquisitionStats, header);
 
417
      MXUserDumpAcquisitionStats(&acquireStats->data, header);
418
418
 
419
 
      if (Atomic_ReadPtr(&stats->acquisitionHisto) != NULL) {
420
 
         MXUserHistoDump(Atomic_ReadPtr(&stats->acquisitionHisto), header);
 
419
      if (Atomic_ReadPtr(&acquireStats->histo) != NULL) {
 
420
         MXUserHistoDump(Atomic_ReadPtr(&acquireStats->histo), header);
421
421
      }
422
422
 
423
423
      /*
424
424
       * Has the semaphore gone "hot"? If so, implement the hot actions.
425
425
       */
426
426
 
427
 
      MXUserKitchen(&stats->acquisitionStats, &contentionRatio, &isHot,
428
 
                    &doLog);
 
427
      MXUserKitchen(&acquireStats->data, &contentionRatio, &isHot, &doLog);
429
428
 
430
429
      if (isHot) {
431
 
         MXUserForceHisto(&stats->acquisitionHisto,
 
430
         MXUserForceHisto(&acquireStats->histo,
432
431
                          MXUSER_STAT_CLASS_ACQUISITION,
433
432
                          MXUSER_DEFAULT_HISTO_MIN_VALUE_NS,
434
433
                          MXUSER_DEFAULT_HISTO_DECADES);
463
462
{
464
463
   MXUserSemaphore *sema = (MXUserSemaphore *) header;
465
464
 
466
 
   Warning("%s: semaphore @ 0x%p\n", __FUNCTION__, sema);
 
465
   Warning("%s: semaphore @ %p\n", __FUNCTION__, sema);
467
466
 
468
467
   Warning("\tsignature 0x%X\n", sema->header.signature);
469
468
   Warning("\tname %s\n", sema->header.name);
471
470
   Warning("\tserial number %u\n", sema->header.serialNumber);
472
471
 
473
472
   Warning("\treference count %u\n", Atomic_Read(&sema->activeUserCount));
474
 
   Warning("\taddress of native semaphore 0x%p\n", &sema->nativeSemaphore);
 
473
   Warning("\taddress of native semaphore %p\n", &sema->nativeSemaphore);
475
474
}
476
475
 
477
476
 
511
510
   }
512
511
 
513
512
   if (LIKELY(MXUserInit(&sema->nativeSemaphore) == 0)) {
514
 
      MXUserStats *stats;
515
 
 
516
513
      sema->header.signature = MXUserGetSignature(MXUSER_TYPE_SEMA);
517
514
      sema->header.name = properName;
518
515
      sema->header.rank = rank;
519
516
      sema->header.serialNumber = MXUserAllocSerialNumber();
520
517
      sema->header.dumpFunc = MXUserDumpSemaphore;
521
518
 
522
 
      if (MXUserStatsEnabled()) {
 
519
      if (MXUserStatsMode() == 0) {
 
520
         sema->header.statsFunc = NULL;
 
521
         Atomic_WritePtr(&sema->acquireStatsMem, NULL);
 
522
      } else {
 
523
         MXUserAcquireStats *acquireStats;
 
524
 
 
525
         acquireStats = Util_SafeCalloc(1, sizeof(*acquireStats));
 
526
 
 
527
         MXUserAcquisitionStatsSetUp(&acquireStats->data);
 
528
 
523
529
         sema->header.statsFunc = MXUserStatsActionSema;
524
 
 
525
 
         stats = Util_SafeCalloc(1, sizeof(*stats));
526
 
 
527
 
         MXUserAcquisitionStatsSetUp(&stats->acquisitionStats);
528
 
 
529
 
         Atomic_WritePtr(&sema->statsMem, stats);
530
 
      } else {
531
 
         sema->header.statsFunc = NULL;
532
 
         Atomic_WritePtr(&sema->statsMem, NULL);
 
530
         Atomic_WritePtr(&sema->acquireStatsMem, acquireStats);
533
531
      }
534
532
 
535
533
      MXUserAddToList(&sema->header);
564
562
{
565
563
   if (LIKELY(sema != NULL)) {
566
564
      int err;
567
 
      MXUserStats *stats;
568
565
 
569
566
      MXUserValidateHeader(&sema->header, MXUSER_TYPE_SEMA);
570
567
 
574
571
                            __FUNCTION__);
575
572
      }
576
573
 
 
574
      sema->header.signature = 0;  // just in case...
 
575
 
577
576
      err = MXUserDestroy(&sema->nativeSemaphore);
578
577
 
579
578
      if (UNLIKELY(err != 0)) {
581
580
                            __FUNCTION__, err);
582
581
      }
583
582
 
584
 
      sema->header.signature = 0;  // just in case...
585
 
 
586
583
      MXUserRemoveFromList(&sema->header);
587
584
 
588
 
      stats = (MXUserStats *) Atomic_ReadPtr(&sema->statsMem);
589
 
 
590
 
      if (stats) {
591
 
         MXUserAcquisitionStatsTearDown(&stats->acquisitionStats);
592
 
         MXUserHistoTearDown(Atomic_ReadPtr(&stats->acquisitionHisto));
593
 
 
594
 
         free(stats);
 
585
      if (vmx86_stats) {
 
586
         MXUserAcquireStats *acquireStats;
 
587
 
 
588
         acquireStats = Atomic_ReadPtr(&sema->acquireStatsMem);
 
589
 
 
590
         if (LIKELY(acquireStats != NULL)) {
 
591
            MXUserAcquisitionStatsTearDown(&acquireStats->data);
 
592
            MXUserHistoTearDown(Atomic_ReadPtr(&acquireStats->histo));
 
593
 
 
594
            free(acquireStats);
 
595
         }
595
596
      }
596
597
 
597
598
      free(sema->header.name);
623
624
MXUser_DownSemaphore(MXUserSemaphore *sema)  // IN/OUT:
624
625
{
625
626
   int err;
626
 
   MXUserStats *stats;
627
627
 
628
628
   ASSERT(sema);
629
629
   MXUserValidateHeader(&sema->header, MXUSER_TYPE_SEMA);
632
632
 
633
633
   MXUserAcquisitionTracking(&sema->header, TRUE);  // rank checking
634
634
 
635
 
   stats = (MXUserStats *) Atomic_ReadPtr(&sema->statsMem);
636
 
 
637
 
   if (stats) { 
 
635
   if (vmx86_stats) {
 
636
      VmTimeType start = 0;
638
637
      Bool tryDownSuccess = FALSE;
639
 
      VmTimeType begin = Hostinfo_SystemTimerNS();
 
638
      MXUserAcquireStats *acquireStats;
 
639
 
 
640
      acquireStats = Atomic_ReadPtr(&sema->acquireStatsMem);
 
641
 
 
642
      if (LIKELY(acquireStats != NULL)) {
 
643
         start = Hostinfo_SystemTimerNS();
 
644
      }
640
645
 
641
646
      err = MXUserTryDown(&sema->nativeSemaphore, &tryDownSuccess);
642
647
 
644
649
         if (!tryDownSuccess) {
645
650
            err = MXUserDown(&sema->nativeSemaphore);
646
651
         }
647
 
 
648
 
         if (LIKELY(err == 0)) {
649
 
            MXUserHisto *histo;
650
 
            VmTimeType value = Hostinfo_SystemTimerNS() - begin;
651
 
 
652
 
            MXUserAcquisitionSample(&stats->acquisitionStats, TRUE,
653
 
                                    !tryDownSuccess, value);
654
 
 
655
 
            histo = Atomic_ReadPtr(&stats->acquisitionHisto);
656
 
 
657
 
            if (UNLIKELY(histo != NULL)) {
658
 
               MXUserHistoSample(histo, value, GetReturnAddress());
659
 
            }
 
652
      }
 
653
 
 
654
      if (LIKELY((err == 0) && (acquireStats != NULL))) {
 
655
         MXUserHisto *histo;
 
656
         VmTimeType value = Hostinfo_SystemTimerNS() - start;
 
657
 
 
658
         MXUserAcquisitionSample(&acquireStats->data, TRUE,
 
659
                                 !tryDownSuccess, value);
 
660
 
 
661
         histo = Atomic_ReadPtr(&acquireStats->histo);
 
662
 
 
663
         if (UNLIKELY(histo != NULL)) {
 
664
            MXUserHistoSample(histo, value, GetReturnAddress());
660
665
         }
661
666
      }
662
667
   } else {
698
703
                          uint32 msecWait)        // IN:
699
704
{
700
705
   int err;
701
 
   MXUserStats *stats;
702
706
   Bool downOccurred = FALSE;
703
707
 
704
708
   ASSERT(sema);
708
712
 
709
713
   MXUserAcquisitionTracking(&sema->header, TRUE);  // rank checking
710
714
 
711
 
   stats = (MXUserStats *) Atomic_ReadPtr(&sema->statsMem);
712
 
 
713
 
   if (stats) { 
 
715
   if (vmx86_stats) {
 
716
      VmTimeType start = 0;
714
717
      Bool tryDownSuccess = FALSE;
715
 
      VmTimeType begin = Hostinfo_SystemTimerNS();
 
718
      MXUserAcquireStats *acquireStats;
 
719
 
 
720
      acquireStats = Atomic_ReadPtr(&sema->acquireStatsMem);
 
721
 
 
722
      if (LIKELY(acquireStats != NULL)) {
 
723
         start = Hostinfo_SystemTimerNS();
 
724
      }
716
725
 
717
726
      err = MXUserTryDown(&sema->nativeSemaphore, &tryDownSuccess);
718
727
 
723
732
            err = MXUserTimedDown(&sema->nativeSemaphore, msecWait,
724
733
                                  &downOccurred);
725
734
         }
726
 
 
727
 
         if (LIKELY(err == 0)) {
728
 
            VmTimeType value = Hostinfo_SystemTimerNS() - begin;
729
 
 
730
 
            MXUserAcquisitionSample(&stats->acquisitionStats, downOccurred,
731
 
                                    !tryDownSuccess, value);
732
 
 
733
 
            if (downOccurred) {
734
 
               MXUserHisto *histo = Atomic_ReadPtr(&stats->acquisitionHisto);
735
 
 
736
 
               if (UNLIKELY(histo != NULL)) {
737
 
                  MXUserHistoSample(histo, value, GetReturnAddress());
738
 
               }
 
735
      }
 
736
 
 
737
      if (LIKELY((err == 0) && (acquireStats != NULL))) {
 
738
         VmTimeType value = Hostinfo_SystemTimerNS() - start;
 
739
 
 
740
         MXUserAcquisitionSample(&acquireStats->data, downOccurred,
 
741
                                 !tryDownSuccess, value);
 
742
 
 
743
         if (downOccurred) {
 
744
            MXUserHisto *histo = Atomic_ReadPtr(&acquireStats->histo);
 
745
 
 
746
            if (UNLIKELY(histo != NULL)) {
 
747
               MXUserHistoSample(histo, value, GetReturnAddress());
739
748
            }
740
749
         }
741
750
      }
782
791
MXUser_TryDownSemaphore(MXUserSemaphore *sema)  // IN/OUT:
783
792
{
784
793
   int err;
785
 
   MXUserStats *stats;
786
794
   Bool downOccurred = FALSE;
787
795
 
788
796
   ASSERT(sema);
797
805
                         __FUNCTION__, err);
798
806
   }
799
807
 
800
 
   stats = (MXUserStats *) Atomic_ReadPtr(&sema->statsMem);
801
 
 
802
 
   if (stats) {
803
 
      MXUserAcquisitionSample(&stats->acquisitionStats, downOccurred,
804
 
                              !downOccurred, 0ULL);
 
808
   if (vmx86_stats) {
 
809
      MXUserAcquireStats *acquireStats;
 
810
 
 
811
      acquireStats = Atomic_ReadPtr(&sema->acquireStatsMem);
 
812
 
 
813
      if (LIKELY(acquireStats != NULL)) {
 
814
         MXUserAcquisitionSample(&acquireStats->data, downOccurred,
 
815
                                 !downOccurred, 0ULL);
 
816
      }
805
817
   }
806
818
 
807
819
   Atomic_Dec(&sema->activeUserCount);
877
889
 
878
890
   ASSERT(semaStorage);
879
891
 
880
 
   sema = (MXUserSemaphore *) Atomic_ReadPtr(semaStorage);
 
892
   sema = Atomic_ReadPtr(semaStorage);
881
893
 
882
894
   if (UNLIKELY(sema == NULL)) {
883
895
      MXUserSemaphore *newSema = MXUser_CreateSemaphore(name, rank);
884
896
 
885
 
      sema = (MXUserSemaphore *) Atomic_ReadIfEqualWritePtr(semaStorage,
886
 
                                                            NULL,
887
 
                                                            (void *) newSema);
 
897
      sema = Atomic_ReadIfEqualWritePtr(semaStorage, NULL, (void *) newSema);
888
898
 
889
899
      if (sema) {
890
900
         MXUser_DestroySemaphore(newSema);
891
901
      } else {
892
 
         sema = (MXUserSemaphore *) Atomic_ReadPtr(semaStorage);
 
902
         sema = Atomic_ReadPtr(semaStorage);
893
903
      }
894
904
   }
895
905