Reputation: 96
Is it possible to have Perl run a Linux OS function with a modified scheduling and/or IO scheduling priority without external commands? I am trying to simulate the following:
nice -n19 ionice -c2 -n7 cp largefile largefile2
Can I somehow do this with File::Copy, the setpriority function, and the CPAN module Linux::IO_Prio? Would I just need to lower the scheduling priority of $0?
EDIT: If I do the following will the priority and IO be lowered for copy()? Is there a better way to do this?
use Linux::IO_Prio qw(:all);
use File::Copy;
setpriority(0, 0, -20);
ionice(IOPRIO_WHO_PROCESS, $$, IOPRIO_CLASS_IDLE, 7);
copy("file1","file2") or die "Copy failed: $!";
Upvotes: 4
Views: 476
Reputation: 39158
Refining Oesor’s answer:
use BSD::Resource qw(PRIO_PROCESS setpriority);
use Linux::IO_Prio qw(IOPRIO_WHO_PROCESS IOPRIO_PRIO_VALUE IOPRIO_CLASS_BE ioprio_set);
BEGIN { require autodie::hints; autodie::hints->set_hints_for(\&ioprio_set, { fail => sub { $_[0] == -1 } } ) };
use autodie qw(:all setpriority ioprio_set);
setpriority(
PRIO_PROCESS, # 1
$$,
19
);
ioprio_set(
IOPRIO_WHO_PROCESS, # 1
$$,
IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, 7) # 0x4007
);
By the way, you can find out library call and similar stuff with strace
.
Upvotes: 1
Reputation: 6642
You're probably best off simply changing the priority of the currently running pid as needed. Not portable, of course, but doing this is in and of itself non-portable. Anything performing this sort of thing is going to boil down to making the same library calls the external commands do.
my $pid = $$;
`ionice -c2 -p$pid`;
`renice +19 $pid`;
Upvotes: 1