Project

General

Profile

Feature #4038 ยป IO-advise-6-kosaki.patch

kosaki (Motohiro KOSAKI), 12/16/2010 03:44 AM

View differences:

configure.in
1290 1290
fi
1291 1291
AC_CHECK_FUNCS(fmod killpg wait4 waitpid fork spawnv syscall chroot getcwd eaccess\
1292 1292
	      truncate ftruncate chsize times utimes utimensat fcntl lockf lstat\
1293
	      link symlink readlink readdir_r fsync fdatasync fchown\
1293
	      link symlink readlink readdir_r fsync fdatasync fchown posix_fadvise\
1294 1294
	      setitimer setruid seteuid setreuid setresuid setproctitle socketpair\
1295 1295
	      setrgid setegid setregid setresgid issetugid pause lchown lchmod\
1296 1296
	      getpgrp setpgrp getpgid setpgid initgroups getgroups setgroups\
io.c
1

  
1 2
/**********************************************************************
2 3

  
3 4
  io.c -
......
7389 7390
}
7390 7391
#endif
7391 7392

  
7393
struct io_advise_struct {
7394
    int fd;
7395
    off_t offset;
7396
    off_t len;
7397
    int advice;
7398
};
7399

  
7400
static VALUE sym_normal,   sym_sequential, sym_random,
7401
             sym_willneed, sym_dontneed, sym_noreuse;
7402

  
7403
static VALUE
7404
io_advise_internal(void *arg)
7405
{
7406
    struct io_advise_struct *ptr = arg;
7407
    return posix_fadvise(ptr->fd, ptr->offset, ptr->len, ptr->advice);
7408
}
7409

  
7410
static VALUE io_advise_sym_to_const(VALUE sym)
7411
{
7412
#ifdef POSIX_FADV_NORMAL
7413
  if (sym == sym_normal)
7414
      return INT2NUM(POSIX_FADV_NORMAL);
7415
#endif
7416

  
7417
#ifdef POSIX_FADV_RANDOM
7418
  if (sym == sym_random)
7419
      return INT2NUM(POSIX_FADV_RANDOM);
7420
#endif
7421

  
7422
#ifdef POSIX_FADV_SEQUENTIAL
7423
  if (sym == sym_sequential)
7424
      return INT2NUM(POSIX_FADV_SEQUENTIAL);
7425
#endif
7426

  
7427
#ifdef POSIX_FADV_WILLNEED
7428
  if (sym == sym_willneed)
7429
      return INT2NUM(POSIX_FADV_WILLNEED);
7430
#endif
7431

  
7432
#ifdef POSIX_FADV_DONTNEED
7433
  if (sym == sym_dontneed)
7434
      return INT2NUM(POSIX_FADV_DONTNEED);
7435
#endif
7436

  
7437
#ifdef POSIX_FADV_NOREUSE
7438
  if (sym == sym_noreuse)
7439
      return INT2NUM(POSIX_FADV_NOREUSE);
7440
#endif
7441

  
7442
      return Qnil;
7443
}
7444

  
7445
/*
7446
 *  call-seq:
7447
 *     ios.advise(advice, offset=0, len=0) -> nil
7448
 *
7449
 *  Announce an intention to access data from the current file in a
7450
 *  specific pattern. On platforms that do not support the
7451
 *  <em>posix_fadvise(2)</em> system call, this method is a no-op.
7452
 *
7453
 * _advice_ is one of the following symbols:
7454
 *
7455
 *  * :normal - No advice to give; the default assumption for an open file.
7456
 *  * :sequential - The data will be accessed sequentially:
7457
 *     with lower offsets read before higher ones.
7458
 *  * :random - The data will be accessed in random order.
7459
 *  * :willneed - The data will be accessed in the near future.
7460
 *  * :dontneed - The data will not be accessed in the near future.
7461
 *  * :noreuse - The data will only be accessed once.
7462
 *
7463
 * The semantics of a piece of advice are platform-dependent. See
7464
 * <em>man 2 posix_fadvise</em> for details.
7465
 *
7466
 *  "data" means the region of the current file that begins at
7467
 *  _offset_ and extends for _len_ bytes. If _len_ is 0, the region
7468
 *  ends at the last byte of the file. By default, both _offset_ and
7469
 *  _len_ are 0, meaning that the advice applies to the entire file.
7470
 *
7471
 *  If an error occurs, one of the following exceptions will be raised:
7472
 *
7473
 *  * <code>IOError</code> - The <code>IO</code> stream is closed.
7474
 *  * <code>Errno::EBADF</code> - The file descriptor of the current file is
7475
      invalid.
7476
 *  * <code>Errno::EINVAL</code> - An invalid value for _advice_ was given.
7477
 *  * <code>Errno::ESPIPE</code> - The file descriptor of the current
7478
 *  * file refers to a FIFO or pipe. (Linux raises <code>Errno::EINVAL</code>
7479
 *  * in this case).
7480
 *  * <code>TypeError</code> - Either _advice_ was not a Symbol, or one of the
7481
      other arguments was not an <code>Integer</code>.
7482
 *  * <code>RangeError</code> - One of the arguments given was too big/small.
7483
 *
7484
 * This list is not exhaustive; other Errno:: exceptions are also possible.
7485
 */
7486
static VALUE
7487
rb_io_advise(int argc, VALUE *argv, VALUE io)
7488
{
7489
#ifdef HAVE_POSIX_FADVISE
7490
  int rv;
7491
  VALUE initadvice, initoffset, initlen;
7492
  rb_io_t *fptr;
7493
  struct io_advise_struct ias;
7494
  VALUE advice;
7495

  
7496
  rb_scan_args(argc, argv, "12", &initadvice, &initoffset, &initlen);
7497
  if (TYPE(initadvice) != T_SYMBOL)
7498
      rb_raise(rb_eTypeError, "advice must be a Symbol");
7499

  
7500
  advice = io_advise_sym_to_const(initadvice);
7501

  
7502
  /*
7503
   * The platform doesn't support this hint. We don't raise exception, instead
7504
   * silently ignore it. Because IO::advise is only hint.
7505
   */
7506
  if (advice == Qnil)
7507
      return Qnil;
7508

  
7509
  io = GetWriteIO(io);
7510
  GetOpenFile(io, fptr);
7511

  
7512
  ias.fd     = fptr->fd;
7513
  ias.advice = NUM2INT(advice);
7514
  ias.offset = NIL_P(initoffset) ? 0 : NUM2OFFT(initoffset);
7515
  ias.len    = NIL_P(initlen)    ? 0 : NUM2OFFT(initlen);
7516

  
7517
  if (rv = (int)rb_thread_blocking_region(io_advise_internal, &ias, RUBY_UBF_IO, 0))
7518
      /* posix_fadvise(2) doesn't set errno. On success it returns 0; otherwise
7519
	 it returns the error code. */
7520
      rb_syserr_fail(rv, RSTRING_PTR(fptr->pathv));
7521

  
7522
  return Qnil;
7523

  
7524
#else /* HAVE_POSIX_FADVISE */
7525

  
7526
  /* Ignore all hint */
7527
  return Qnil;
7528
#endif
7529
}
7530

  
7392 7531
/*
7393 7532
 *  call-seq:
7394 7533
 *     IO.select(read_array
......
10142 10281
    rb_define_method(rb_cIO, "binmode",  rb_io_binmode_m, 0);
10143 10282
    rb_define_method(rb_cIO, "binmode?", rb_io_binmode_p, 0);
10144 10283
    rb_define_method(rb_cIO, "sysseek", rb_io_sysseek, -1);
10284
    rb_define_method(rb_cIO, "advise", rb_io_advise, -1);
10145 10285

  
10146 10286
    rb_define_method(rb_cIO, "ioctl", rb_io_ioctl, -1);
10147 10287
    rb_define_method(rb_cIO, "fcntl", rb_io_fcntl, -1);
......
10318 10458
    sym_textmode = ID2SYM(rb_intern("textmode"));
10319 10459
    sym_binmode = ID2SYM(rb_intern("binmode"));
10320 10460
    sym_autoclose = ID2SYM(rb_intern("autoclose"));
10461
    sym_normal = ID2SYM(rb_intern("normal"));
10462
    sym_sequential = ID2SYM(rb_intern("sequential"));
10463
    sym_random = ID2SYM(rb_intern("random"));
10464
    sym_willneed = ID2SYM(rb_intern("willneed"));
10465
    sym_dontneed = ID2SYM(rb_intern("dontneed"));
10466
    sym_noreuse = ID2SYM(rb_intern("noreuse"));
10321 10467
}
test/ruby/test_io.rb
1722 1722
      end
1723 1723
    end
1724 1724
  end
1725

  
1726
  def test_advise
1727
    t = make_tempfile
1728
    assert_raise(ArgumentError, "no arguments") { t.advise }
1729
    %w{normal random sequential willneed dontneed noreuse}.map(&:to_sym).each do |adv|
1730
      [[0,0], [0, 20], [400, 2]].each do |offset, len|
1731
        open(make_tempfile.path) do |t|
1732
          assert_equal(t.advise(adv, offset, len), nil)
1733
          assert_raise(ArgumentError, "superfluous arguments") do
1734
            t.advise(adv, offset, len, offset)
1735
          end
1736
          assert_raise(TypeError, "wrong type for first argument") do
1737
            t.advise(adv.to_s, offset, len)
1738
          end
1739
          assert_raise(TypeError, "wrong type for last argument") do
1740
            t.advise(adv, offset, Array(len))
1741
          end
1742
          assert_raise(RangeError, "last argument too big") do
1743
            t.advise(adv, offset, 9999e99)
1744
          end
1745
        end
1746
        assert_raise(IOError, "closed file") do
1747
          make_tempfile.advise(adv.to_sym, offset, len)
1748
        end
1749
      end
1750
    end
1751
    %w{Normal rand glark will_need zzzzzzzzzzzz \u2609}.map(&:to_sym).each do |adv|
1752
      [[0,0], [0, 20], [400, 2]].each do |offset, len|
1753
        open(make_tempfile.path) do |t|
1754
          assert_equal(t.advise(adv, offset, len), nil)
1755
        end
1756
      end
1757
    end
1758
  end
1725 1759
end