~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"
18
#include <m_string.h>
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
  DBUG_ENTER("set_max_open_files");
46
  DBUG_PRINT("enter",("files: %u", max_file_limit));
47
48
  if (!getrlimit(RLIMIT_NOFILE,&rlimit))
49
  {
50
    old_cur= (uint) rlimit.rlim_cur;
51
    DBUG_PRINT("info", ("rlim_cur: %u  rlim_max: %u",
52
			(uint) rlimit.rlim_cur,
53
			(uint) rlimit.rlim_max));
54
    if (rlimit.rlim_cur == RLIM_INFINITY)
55
      rlimit.rlim_cur = max_file_limit;
56
    if (rlimit.rlim_cur >= max_file_limit)
57
      DBUG_RETURN(rlimit.rlim_cur);		/* purecov: inspected */
58
    rlimit.rlim_cur= rlimit.rlim_max= max_file_limit;
59
    if (setrlimit(RLIMIT_NOFILE, &rlimit))
60
      max_file_limit= old_cur;			/* Use original value */
61
    else
62
    {
63
      rlimit.rlim_cur= 0;			/* Safety if next call fails */
64
      (void) getrlimit(RLIMIT_NOFILE,&rlimit);
65
      DBUG_PRINT("info", ("rlim_cur: %u", (uint) rlimit.rlim_cur));
66
      if (rlimit.rlim_cur)			/* If call didn't fail */
67
	max_file_limit= (uint) rlimit.rlim_cur;
68
    }
69
  }
70
  DBUG_PRINT("exit",("max_file_limit: %u", max_file_limit));
71
  DBUG_RETURN(max_file_limit);
72
}
73
74
#else
75
static int set_max_open_files(uint max_file_limit)
76
{
77
  /* We don't know the limit. Return best guess */
78
  return min(max_file_limit, OS_FILE_LIMIT);
79
}
80
#endif
81
82
83
/*
84
  Change number of open files
85
86
  SYNOPSIS:
87
    my_set_max_open_files()
88
    files		Number of requested files
89
90
  RETURN
91
    number of files available for open
92
*/
93
94
uint my_set_max_open_files(uint files)
95
{
96
  struct st_my_file_info *tmp;
97
  DBUG_ENTER("my_set_max_open_files");
98
  DBUG_PRINT("enter",("files: %u  my_file_limit: %u", files, my_file_limit));
99
100
  files= set_max_open_files(min(files, OS_FILE_LIMIT));
101
  if (files <= MY_NFILE)
102
    DBUG_RETURN(files);
103
104
  if (!(tmp= (struct st_my_file_info*) my_malloc(sizeof(*tmp) * files,
105
						 MYF(MY_WME))))
106
    DBUG_RETURN(MY_NFILE);
107
108
  /* Copy any initialized files */
109
  memcpy((char*) tmp, (char*) my_file_info,
110
         sizeof(*tmp) * min(my_file_limit, files));
111
  bzero((char*) (tmp + my_file_limit),
112
        max((int) (files- my_file_limit), 0)*sizeof(*tmp));
113
  my_free_open_file_info();			/* Free if already allocated */
114
  my_file_info= tmp;
115
  my_file_limit= files;
116
  DBUG_PRINT("exit",("files: %u", files));
117
  DBUG_RETURN(files);
118
}
119
120
121
void my_free_open_file_info()
122
{
123
  DBUG_ENTER("my_free_file_info");
124
  if (my_file_info != my_file_info_default)
125
  {
126
    /* Copy data back for my_print_open_files */
127
    memcpy((char*) my_file_info_default, my_file_info,
128
           sizeof(*my_file_info_default)* MY_NFILE);
129
    my_free((char*) my_file_info, MYF(0));
130
    my_file_info= my_file_info_default;
131
    my_file_limit= MY_NFILE;
132
  }
133
  DBUG_VOID_RETURN;
134
}