~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2000 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
16
#include "mysys_priv.h"
17
#include "my_static.h"
212.5.18 by Monty Taylor
Moved m_ctype, m_string and my_bitmap. Removed t_ctype.
18
#include <mystrings/m_string.h>
1 by brian
clean slate
19
20
/*
21
  set how many open files we want to be able to handle
22
23
  SYNOPSIS
24
    set_maximum_open_files()
25
    max_file_limit		Files to open
26
27
  NOTES
28
    The request may not fulfilled becasue of system limitations
29
30
  RETURN
31
    Files available to open.
32
    May be more or less than max_file_limit!
33
*/
34
35
#if defined(HAVE_GETRLIMIT) && defined(RLIMIT_NOFILE)
36
37
#ifndef RLIM_INFINITY
38
#define RLIM_INFINITY ((uint) 0xffffffff)
39
#endif
40
41
static uint set_max_open_files(uint max_file_limit)
42
{
43
  struct rlimit rlimit;
44
  uint old_cur;
45
46
  if (!getrlimit(RLIMIT_NOFILE,&rlimit))
47
  {
48
    old_cur= (uint) rlimit.rlim_cur;
49
    if (rlimit.rlim_cur == RLIM_INFINITY)
50
      rlimit.rlim_cur = max_file_limit;
51
    if (rlimit.rlim_cur >= max_file_limit)
51.3.19 by Jay Pipes
Phase 6 - Remove DBUG from mysys
52
      return(rlimit.rlim_cur);		/* purecov: inspected */
1 by brian
clean slate
53
    rlimit.rlim_cur= rlimit.rlim_max= max_file_limit;
54
    if (setrlimit(RLIMIT_NOFILE, &rlimit))
55
      max_file_limit= old_cur;			/* Use original value */
56
    else
57
    {
58
      rlimit.rlim_cur= 0;			/* Safety if next call fails */
59
      (void) getrlimit(RLIMIT_NOFILE,&rlimit);
60
      if (rlimit.rlim_cur)			/* If call didn't fail */
61
	max_file_limit= (uint) rlimit.rlim_cur;
62
    }
63
  }
51.3.19 by Jay Pipes
Phase 6 - Remove DBUG from mysys
64
  return(max_file_limit);
1 by brian
clean slate
65
}
66
67
#else
68
static int set_max_open_files(uint max_file_limit)
69
{
70
  /* We don't know the limit. Return best guess */
398.1.4 by Monty Taylor
Renamed max/min.
71
  return cmin(max_file_limit, OS_FILE_LIMIT);
1 by brian
clean slate
72
}
73
#endif
74
75
76
/*
77
  Change number of open files
78
79
  SYNOPSIS:
80
    my_set_max_open_files()
81
    files		Number of requested files
82
83
  RETURN
84
    number of files available for open
85
*/
86
87
uint my_set_max_open_files(uint files)
88
{
89
  struct st_my_file_info *tmp;
90
398.1.4 by Monty Taylor
Renamed max/min.
91
  files= set_max_open_files(cmin(files, OS_FILE_LIMIT));
1 by brian
clean slate
92
  if (files <= MY_NFILE)
51.3.19 by Jay Pipes
Phase 6 - Remove DBUG from mysys
93
    return(files);
1 by brian
clean slate
94
95
  if (!(tmp= (struct st_my_file_info*) my_malloc(sizeof(*tmp) * files,
96
						 MYF(MY_WME))))
51.3.19 by Jay Pipes
Phase 6 - Remove DBUG from mysys
97
    return(MY_NFILE);
1 by brian
clean slate
98
99
  /* Copy any initialized files */
398.1.4 by Monty Taylor
Renamed max/min.
100
  memcpy(tmp, my_file_info, sizeof(*tmp) * cmin(my_file_limit, files));
212.6.14 by Mats Kindahl
Removing redundant use of casts in mysys for memcmp(), memcpy(), memset(), and memmove().
101
  /*
102
    The int cast is necessary since 'my_file_limits' might be greater
103
    than 'files'.
104
  */
105
  memset(tmp + my_file_limit, 0,
398.1.4 by Monty Taylor
Renamed max/min.
106
         cmax((int) (files - my_file_limit), 0)*sizeof(*tmp));
1 by brian
clean slate
107
  my_free_open_file_info();			/* Free if already allocated */
108
  my_file_info= tmp;
109
  my_file_limit= files;
51.3.19 by Jay Pipes
Phase 6 - Remove DBUG from mysys
110
  return(files);
1 by brian
clean slate
111
}
112
113
114
void my_free_open_file_info()
115
{
116
  if (my_file_info != my_file_info_default)
117
  {
118
    /* Copy data back for my_print_open_files */
212.6.14 by Mats Kindahl
Removing redundant use of casts in mysys for memcmp(), memcpy(), memset(), and memmove().
119
    memcpy(my_file_info_default, my_file_info,
1 by brian
clean slate
120
           sizeof(*my_file_info_default)* MY_NFILE);
121
    my_free((char*) my_file_info, MYF(0));
122
    my_file_info= my_file_info_default;
123
    my_file_limit= MY_NFILE;
124
  }
51.3.19 by Jay Pipes
Phase 6 - Remove DBUG from mysys
125
  return;
1 by brian
clean slate
126
}