/* -*-C-*- */

/*  bench-path.c  */


/*
 * Author: Nikita Danilov <NikitaDanilov@yahoo.COM>
 * Keywords: dafs, vfs, open by inode, open by key
 *
 * Copyright (C) 2000 Namesys (Hans Reiser)
 *
 * This file is part of dafs.
 *
 * Dafs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

#include <stdio.h>
#include <sys/time.h>
#include <unistd.h>
#include <assert.h>
#include <fcntl.h>

typedef int __u32;
struct key {
  __u32 k_dir_id;
  __u32 k_objectid;
  __u32 k_offset;
  __u32 k_uniqueness;
};

#define _LINUX_REISER_FS_H
#define _LINUX_REISER_FS_SB

#include "da.h"

#define BY_PATH  0
#define BY_INODE 1
#define BY_KEY   2

#define MARK     1000

/* $Id$ */
static char cvsid[] = "@(#)$Id$";

double delay( struct timeval start, struct timeval end )
{
  return( ( end.tv_sec - start.tv_sec ) + 
		  ( ( double ) end.tv_usec - ( double ) start.tv_usec ) / 1e6 );
}

int main( int argc, char **argv )
{
  struct timeval start;
  struct timeval mark;
  struct timeval end;
  struct timezone dummy;

  char input[ 4096 ];
  char fileName[ 4096 ];
  char prefix[ 4096 ];

  double freq[ 10000 ];
  double avg;

  int i;
  int samples_taken;
  int fd;
  int n;
  char *buffer;
  int mode;

  setbuf( stdin, NULL );
  setbuf( stdout, NULL );
  setbuf( stderr, NULL );

  assert( argc == 3 );

  if( !strcmp( argv[ 1 ], "path" ) )
	{
	  mode = BY_PATH;
	}
  else if( !strcmp( argv[ 1 ], "inode" ) )
	{
	  mode = BY_INODE;
	  sprintf( prefix, 
			   "/mnt/dafs/" DA_ENTRY_POINT_NAME "/" 
			   DA_OPEN_BY_INODE_NAME"/#%%x" );
	}
  else if( !strcmp( argv[ 1 ], "key" ) )
	{
	  mode = BY_KEY;
	  sprintf( prefix, 
			   "/mnt/dafs/" DA_ENTRY_POINT_NAME "/" 
			   DA_OPEN_BY_KEY_NAME"/#" );
	}
  else
	{
	  fprintf( stderr, "Invalid mode\n" );
	  exit( 1 );
	}

  n = atoi( argv[ 2 ] );
  if( n >= 0 )
	{
	  assert( ( buffer = ( char * ) malloc( n ) ) != NULL );
	}

  printf( "\nMode: %i, n: %i\n", mode, n );
  i = 0;
  samples_taken = 0;
  assert( gettimeofday( &start, &dummy ) != -1 );
  while( !feof( stdin ) && 
		 ( fgets( input, sizeof input, stdin ) != NULL ) )
	{
	  if( input[ strlen( input ) - 1 ] == '\n' )
		{
		  input[ strlen( input ) - 1 ] = '\0';
		}
	  ++i;
	  if( i % MARK == 0 )
		{
		  double time;

		  assert( gettimeofday( &mark, &dummy ) != -1 );
		  time = delay( start, mark );
		  freq[ samples_taken ] = i / time;
		  printf( "%i %f %f\n", i, time, freq[ samples_taken++ ] );
		}
	  switch( mode )
		{
		case BY_PATH:
		  {
			strcpy( fileName, input );
			break;
		  }
		case BY_INODE:
		  {
			unsigned long inode;
			
			inode = atol( input );
			sprintf( fileName, prefix, inode );
			break;
		  }
		case BY_KEY:
		  {
			strcat( strcpy( fileName, prefix ), input );
			break;
		  }
		default:
		  {
			fprintf( stderr, "Invalid mode?!\n" );
			exit( 1 );
		  }
		}
	  if( ( fd = open( fileName, O_RDONLY ) ) == -1 )
		{
		  fprintf( stderr, "cannot open `%s'\n", fileName );
		  perror( "open" );
		  continue;
		}
	  if( ( n >= 0 ) && ( read( fd, buffer, n ) == -1 ) )
		{
		  fprintf( stderr, "cannot read `%s'\n", fileName );
		  perror( "read" );
		}
	  close( fd );
	}
  assert( gettimeofday( &end, &dummy ) != -1 );
  if( n >= 0 )
	{
	  free( buffer );
	}
  avg = 0.0;
  for( i = 0 ; i < samples_taken ; ++i )
	{
	  avg += freq[ i ];
	}
  avg /= samples_taken;
  printf( "Average_freq %f\n", avg );
}

/*
 * $Log$
 */

