2008-05-28 17:28:19 +00:00
#! /bin/sh
2008-09-02 17:51:24 +00:00
DECAY_TIME=30*86400
RUN_INCREMENT=86400
MAX_NRESULTS=10000
FILE_DELETE_READY=1
FILE_DELETE_DONE=2
FILE_DELETE_ERROR=3
2008-05-28 17:28:19 +00:00
dbhost=`grep db_host ../config.xml | tr '[\<\>]' '[ ]' | head -1 | awk '{print $2}'`
2008-07-22 23:36:55 +00:00
replica_dbhost=`grep replica_db_host ../config.xml | tr '[\<\>]' '[ ]' | head -1 | awk '{print $2}'`
2008-05-28 17:28:19 +00:00
dbuser=`grep db_user ../config.xml | tr '[\<\>]' '[ ]' | head -1 | awk '{print $2}'`
dbname=`grep db_name ../config.xml | tr '[\<\>]' '[ ]' | head -1 | awk '{print $2}'`
2008-09-02 17:51:24 +00:00
if test ! -z "${replica_dbhost}" ; then
slave_gap=`mysql -E -h ${replica_dbhost} --execute="show slave status" | grep Seconds_Behind_Master | awk '{print $2}'`
fi
if test -z "${replica_dbhost}" || test "$slave_gap" = "NULL" || test $slave_gap -gt $RUN_INCREMENT ; then
2008-07-22 23:36:55 +00:00
replica_dbhost=${dbhost}
fi
2008-05-28 17:28:19 +00:00
MYSQL="mysql -D $dbname -h $dbhost -u $dbuser -N -B"
2008-07-22 23:36:55 +00:00
MYSQL_R="mysql -D $dbname -h $replica_dbhost -u $dbuser -N -B"
2008-05-28 17:28:19 +00:00
2008-07-22 23:36:55 +00:00
function median_host_query() {
2008-05-28 17:28:19 +00:00
$MYSQL --execute="
create temporary table medians (id int auto_increment primary key)
select $1 from host
where rpc_time>unix_timestamp(now())-($DECAY_TIME)
and credit_per_cpu_sec>0
2008-09-02 17:51:24 +00:00
order by $1;
2008-05-28 17:28:19 +00:00
create temporary table ids
select round(avg(id)-0.5) as id from medians;
insert into ids select round(avg(id)+0.5) from medians;
select avg($1) from medians where id in (
select id from ids
);
drop table medians;
drop table ids;"
}
2008-07-22 23:36:55 +00:00
function get_recent_credited_results() {
$MYSQL_R --execute="
2008-09-02 17:51:24 +00:00
set session transaction isolation level read uncommitted;
2008-07-22 23:36:55 +00:00
create temporary table recent_results
select
granted_credit,
cpu_time,
hostid,
p_fpops,
p_iops
from host,result where
file_delete_state in ($FILE_DELETE_READY,$FILE_DELETE_DONE) and
granted_credit>0 and
received_time>unix_timestamp(now())-$DECAY_TIME and
unix_timestamp(result.mod_time)>unix_timestamp(now())-$RUN_INCREMENT and
host.rpc_time>unix_timestamp(now())-$DECAY_TIME and
appid=$1 and
result.hostid=host.id
limit $MAX_NRESULTS;
create temporary table merged_results
select sum(granted_credit)/sum(cpu_time) as granted_rate,
hostid,
(p_fpops+p_iops)*5.787037037037e-13 as bench_rate
from recent_results
group by hostid;
create temporary table medians (id int auto_increment primary key)
select (bench_rate+1e-10)/(granted_rate+1e-10) as mult
from merged_results
order by mult;
create temporary table ids
select round(avg(id)-0.5) as id from medians;
insert into ids select round(avg(id)+0.5) from medians;
select avg(mult) from medians where id in (
select id from ids
);
drop table ids;
drop table medians;
drop table merged_results;
drop table recent_results;"
}
2008-05-28 17:28:19 +00:00
2008-07-22 23:36:55 +00:00
appids=`$MYSQL --execute="select id from app where deprecated=0 and beta=0"`
for appid in $appids ; do
ratio=`get_recent_credited_results $appid`
nrows=`$MYSQL --execute="select count(id) from credit_multiplier where appid=$appid"`
if [ $nrows = 0 ] ; then
$MYSQL --execute="insert into credit_multiplier values (0,$appid,unix_timestamp(now())-86400,1.0)"
fi
last_value=`$MYSQL --execute="select multiplier from credit_multiplier where appid=$appid and (unix_timestamp(now())-time)=(select min(unix_timestamp(now())-time) from credit_multiplier where appid=$appid)"`
2008-09-02 17:51:24 +00:00
echo -n "appid=${appid} last_value=${last_value}"
2008-07-22 23:36:55 +00:00
last_time=`$MYSQL --execute="select unix_timestamp(now())-time from credit_multiplier where appid=$appid and (unix_timestamp(now())-time)=(select min(unix_timestamp(now())-time) from credit_multiplier where appid=$appid)"`
2008-09-02 17:51:24 +00:00
echo -n " desired_value=${ratio}"
2008-05-28 17:28:19 +00:00
2008-09-02 17:51:24 +00:00
value=`$MYSQL --execute="select ($last_value*(($DECAY_TIME)-$last_time)+$ratio*$last_time)/($DECAY_TIME)"`
echo -n " new_value=${value}"
echo
2008-05-28 17:28:19 +00:00
2008-07-22 23:36:55 +00:00
$MYSQL --execute="insert into credit_multiplier values (0,$appid,unix_timestamp(now()),$value);"
done
2008-05-28 17:28:19 +00:00
exit 0