~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/show.cc

Merge Joe

Show diffs side-by-side

added added

removed removed

Lines of Context:
637
637
  {}
638
638
};
639
639
 
640
 
/*****************************************************************************
641
 
  Status functions
642
 
*****************************************************************************/
643
 
 
644
 
static vector<drizzle_show_var *> all_status_vars;
645
 
static bool status_vars_inited= 0;
646
 
static int show_var_cmp(const void *var1, const void *var2)
647
 
{
648
 
  return strcmp(((drizzle_show_var*)var1)->name, ((drizzle_show_var*)var2)->name);
649
 
}
650
 
 
651
 
class show_var_cmp_functor
652
 
{
653
 
  public:
654
 
  show_var_cmp_functor() { }
655
 
  inline bool operator()(const drizzle_show_var *var1, const drizzle_show_var *var2) const
656
 
  {
657
 
    int val= strcmp(var1->name, var2->name);
658
 
    return (val < 0);
659
 
  }
660
 
};
661
 
 
662
 
class show_var_remove_if
663
 
{
664
 
  public:
665
 
  show_var_remove_if() { }
666
 
  inline bool operator()(const drizzle_show_var *curr) const
667
 
  {
668
 
    return (curr->type == SHOW_UNDEF);
669
 
  }
670
 
};
671
 
 
672
 
drizzle_show_var *getFrontOfStatusVars()
673
 
{
674
 
  return all_status_vars.front();
675
 
}
676
 
 
677
 
/*
678
 
  Adds an array of drizzle_show_var entries to the output of SHOW STATUS
679
 
 
680
 
  SYNOPSIS
681
 
    add_status_vars(drizzle_show_var *list)
682
 
    list - an array of drizzle_show_var entries to add to all_status_vars
683
 
           the last entry must be {0,0,SHOW_UNDEF}
684
 
 
685
 
  NOTE
686
 
    The handling of all_status_vars[] is completely internal, it's allocated
687
 
    automatically when something is added to it, and deleted completely when
688
 
    the last entry is removed.
689
 
 
690
 
    As a special optimization, if add_status_vars() is called before
691
 
    init_status_vars(), it assumes "startup mode" - neither concurrent access
692
 
    to the array nor SHOW STATUS are possible (thus it skips locks and qsort)
693
 
*/
694
 
int add_status_vars(drizzle_show_var *list)
695
 
{
696
 
  int res= 0;
697
 
  if (status_vars_inited)
698
 
    pthread_mutex_lock(&LOCK_status);
699
 
  while (list->name)
700
 
    all_status_vars.insert(all_status_vars.begin(), list++);
701
 
  if (status_vars_inited)
702
 
    sort(all_status_vars.begin(), all_status_vars.end(),
703
 
         show_var_cmp_functor());
704
 
  if (status_vars_inited)
705
 
    pthread_mutex_unlock(&LOCK_status);
706
 
  return res;
707
 
}
708
 
 
709
 
/*
710
 
  Make all_status_vars[] usable for SHOW STATUS
711
 
 
712
 
  NOTE
713
 
    See add_status_vars(). Before init_status_vars() call, add_status_vars()
714
 
    works in a special fast "startup" mode. Thus init_status_vars()
715
 
    should be called as late as possible but before enabling multi-threading.
716
 
*/
717
 
void init_status_vars()
718
 
{
719
 
  status_vars_inited= 1;
720
 
  sort(all_status_vars.begin(), all_status_vars.end(),
721
 
       show_var_cmp_functor());
722
 
}
723
 
 
724
 
void reset_status_vars()
725
 
{
726
 
  vector<drizzle_show_var *>::iterator p;
727
 
 
728
 
  p= all_status_vars.begin();
729
 
  while (p != all_status_vars.end())
730
 
  {
731
 
    /* Note that SHOW_LONG_NOFLUSH variables are not reset */
732
 
    if ((*p)->type == SHOW_LONG)
733
 
      (*p)->value= 0;
734
 
    ++p;
735
 
  }
736
 
}
737
 
 
738
 
/*
739
 
  catch-all cleanup function, cleans up everything no matter what
740
 
 
741
 
  DESCRIPTION
742
 
    This function is not strictly required if all add_to_status/
743
 
    remove_status_vars are properly paired, but it's a safety measure that
744
 
    deletes everything from the all_status_vars vector even if some
745
 
    remove_status_vars were forgotten
746
 
*/
747
 
void free_status_vars()
748
 
{
749
 
  all_status_vars.clear();
750
 
}
751
 
 
752
 
/*
753
 
  Removes an array of drizzle_show_var entries from the output of SHOW STATUS
754
 
 
755
 
  SYNOPSIS
756
 
    remove_status_vars(drizzle_show_var *list)
757
 
    list - an array of drizzle_show_var entries to remove to all_status_vars
758
 
           the last entry must be {0,0,SHOW_UNDEF}
759
 
 
760
 
  NOTE
761
 
    there's lots of room for optimizing this, especially in non-sorted mode,
762
 
    but nobody cares - it may be called only in case of failed plugin
763
 
    initialization in the mysqld startup.
764
 
*/
765
 
 
766
 
void remove_status_vars(drizzle_show_var *list)
767
 
{
768
 
  if (status_vars_inited)
769
 
  {
770
 
    pthread_mutex_lock(&LOCK_status);
771
 
    drizzle_show_var *all= all_status_vars.front();
772
 
    int a= 0, b= all_status_vars.size(), c= (a+b)/2;
773
 
 
774
 
    for (; list->name; list++)
775
 
    {
776
 
      int res= 0;
777
 
      for (a= 0, b= all_status_vars.size(); b-a > 1; c= (a+b)/2)
778
 
      {
779
 
        res= show_var_cmp(list, all+c);
780
 
        if (res < 0)
781
 
          b= c;
782
 
        else if (res > 0)
783
 
          a= c;
784
 
        else
785
 
          break;
786
 
      }
787
 
      if (res == 0)
788
 
        all[c].type= SHOW_UNDEF;
789
 
    }
790
 
    /* removes all the SHOW_UNDEF elements from the vector */
791
 
    all_status_vars.erase(std::remove_if(all_status_vars.begin(),
792
 
                            all_status_vars.end(),show_var_remove_if()),
793
 
                            all_status_vars.end());
794
 
    pthread_mutex_unlock(&LOCK_status);
795
 
  }
796
 
  else
797
 
  {
798
 
    drizzle_show_var *all= all_status_vars.front();
799
 
    uint32_t i;
800
 
    for (; list->name; list++)
801
 
    {
802
 
      for (i= 0; i < all_status_vars.size(); i++)
803
 
      {
804
 
        if (show_var_cmp(list, all+i))
805
 
          continue;
806
 
        all[i].type= SHOW_UNDEF;
807
 
        break;
808
 
      }
809
 
    }
810
 
    /* removes all the SHOW_UNDEF elements from the vector */
811
 
    all_status_vars.erase(std::remove_if(all_status_vars.begin(),
812
 
                            all_status_vars.end(),show_var_remove_if()),
813
 
                            all_status_vars.end());
814
 
  }
815
 
}
816
 
 
817
 
/* collect status for all running threads */
818
 
 
819
 
void calc_sum_of_all_status(system_status_var *to)
820
 
{
821
 
  /* Ensure that thread id not killed during loop */
822
 
  pthread_mutex_lock(&LOCK_thread_count); // For unlink from list
823
 
 
824
 
  /* Get global values as base */
825
 
  *to= global_status_var;
826
 
 
827
 
  /* Add to this status from existing threads */
828
 
  for(SessionList::iterator it= getSessionList().begin(); it != getSessionList().end(); ++it )
829
 
  {
830
 
    add_to_status(to, &((*it)->status_var));
831
 
  }
832
 
 
833
 
  pthread_mutex_unlock(&LOCK_thread_count);
834
 
  return;
835
 
}
836
 
 
837
640
} /* namespace drizzled */