Ejemplo n.º 1
0
/**
 * Entry point
 */
int main(int argc, char **argv)
{
   static const char* cmds[] = {
      "b", "buffer",      "1", SETTINGR("Buffer size [KW], default: ",COLLECT_BUFSIZE),
      "d", "data",        "1", SETTINGR("Data cache size [KB], with fallback to: ", GENERIC_DCACHE ),
      "i", "inst",        "1", SETTINGR("Instruction cache size [KB], with fallback to: ", GENERIC_ICACHE),
      "f", "file",        "1", "Sample output file,  default: '" OUTPUT_DEFAULT "', '-' for stdout",
      "F", "Foreground",  "0", "Run daemon in foreground",
      "r", "run",         "1", "0=daemon, 1=config info, >1=<r>KB sample",
      "n", "number",      "1", "Output size in [k|m|g|t] bytes, 0 = unlimited to stdout",
      "o", "onlinetest",  "1", "[t<x>][c<x>] x=[a[n][w]][b[w]] 't'ot, 'c'ontinuous, default: ta8b",
      "p", "pidfile",     "1", "daemon pidfile, default: " PID_DEFAULT ,
      "s", "source",      "1", "Injection source file, default: '" INPUT_DEFAULT "', '-' for stdin",
      "t", "threads",     "1", "Number of threads",
      "v", "verbose",     "1", "Verbose mask 0=none,1=summary,2=retries,4=timing,8=loop,16=code,32=test",
      "w", "write",       "1", "Set write_wakeup_threshold [bits]",
      "h", "help",        "0", "This help"
      };
   static int nopts = sizeof(cmds)/(4*sizeof(char *));
   struct option long_options[nopts+1];
   char short_options[1+nopts*2];
   int c,i,j;
   H_UINT bufct, bufrem, ierr;
   H_PARAMS cmd;

   if (havege_version(HAVEGE_PREP_VERSION)==NULL)
      error_exit("version conflict %s!=%s", HAVEGE_PREP_VERSION, havege_version(NULL));
#if NO_DAEMON==1
   params->setup |= RUN_AS_APP;
#endif
#ifdef  RAW_IN_ENABLE
#define DIAG_USAGE2 SETTINGL("=inject ticks,", DIAG_RUN_INJECT)\
  SETTINGL("=inject data", DIAG_RUN_TEST)

   params->setup |= INJECT | RUN_AS_APP;
#else
#define DIAG_USAGE2 ""
#endif
#ifdef  RAW_OUT_ENABLE
#define DIAG_USAGE1 SETTINGL("=capture,", DIAG_RUN_CAPTURE)

   params->setup |= CAPTURE | RUN_AS_APP;
#else
#define DIAG_USAGE1 ""
#endif
#if NUMBER_CORES>1
   params->setup |= MULTI_CORE;
#endif
#ifdef SIGHUP
   signal(SIGHUP, tidy_exit);
#endif
   signal(SIGINT, tidy_exit);
   signal(SIGTERM, tidy_exit);
   strcpy(short_options,"");
   bufct  = bufrem = 0;
  /**
   * Build options
   */
   for(i=j=0;j<(nopts*4);j+=4) {
      switch(cmds[j][0]) {
         case 'o':
#ifdef  ONLINE_TESTS_ENABLE
            break;
#else
            continue;
#endif
         case 'r':
#if defined(RAW_IN_ENABLE) || defined (RAW_OUT_ENABLE)
            if (0!=(params->setup & (INJECT|CAPTURE))) {
              params->daemon = "havege_diagnostic";
              cmds[j+3] = "run level, 0=diagnostic off,1=config info," DIAG_USAGE1 DIAG_USAGE2 ;
              }
            else
#endif
            if (0!=(params->setup & RUN_AS_APP))
               continue;
            break;
         case 's':
            if (0 == (params->setup & INJECT))
               continue;
            break;
         case 't':
            if (0 == (params->setup & MULTI_CORE))
               continue;
            break;
         case 'p':   case 'w':  case 'F':
            if (0 !=(params->setup & RUN_AS_APP))
               continue;
            break;
         }
      long_options[i].name      = cmds[j+1];
      long_options[i].has_arg   = atoi(cmds[j+2]);
      long_options[i].flag      = NULL;
      long_options[i].val       = cmds[j][0];
      strcat(short_options,cmds[j]);
      if (long_options[i].has_arg!=0) strcat(short_options,":");
      i += 1;
      }
   memset(&long_options[i], 0, sizeof(struct option));

   do {
      c = getopt_long (argc, argv, short_options, long_options, NULL);
      switch(c) {
         case 'F':
            params->setup |= RUN_IN_FG;
            params->foreground = 1;
            break;
         case 'b':
            params->buffersz = ATOU(optarg) * 1024;
            if (params->buffersz<4)
               error_exit("invalid size %s", optarg);
            break;
         case 'd':
            params->d_cache = ATOU(optarg);
            break;
         case 'i':
            params->i_cache = ATOU(optarg);
            break;
         case 'f':
            params->sample_out = optarg;
            if (strcmp(optarg,"-") == 0 )
               params->setup |= USE_STDOUT;
            break;
         case 'n':
            if (get_runsize(&bufct, &bufrem, optarg))
               error_exit("invalid count: %s", optarg);
            params->setup |= RUN_AS_APP|RANGE_SPEC;
            if (bufct==0 && bufrem==0)
               params->setup |= USE_STDOUT;             /* ugly but documented behavior! */
            break;
         case 'o':
            params->tests_config = optarg;
            break;
         case 'p':
            params->pid_file = optarg;
            break;
         case 'r':
            params->run_level  = ATOU(optarg);
            if (params->run_level != 0)
               params->setup |= RUN_AS_APP;
            break;
         case 's':
            params->sample_in = optarg;
            break;
         case 't':
            params->ncores = ATOU(optarg);
            if (params->ncores > NUMBER_CORES)
               error_exit("invalid thread count: %s", optarg);
            break;
         case 'v':
            params->verbose  = ATOU(optarg);
            break;
         case 'w':
            params->setup |= SET_LWM;
            params->low_water = ATOU(optarg);
            break;
         case '?':
         case 'h':
            usage(0, nopts, long_options, cmds);
         case -1:
            break;
         }
      } while (c!=-1);
   if (params->tests_config == 0)
     params->tests_config = (0 != (params->setup & RUN_AS_APP))? TESTS_DEFAULT_APP : TESTS_DEFAULT_RUN;
   memset(&cmd, 0, sizeof(H_PARAMS));
   cmd.collectSize = params->buffersz;
   cmd.icacheSize  = params->i_cache;
   cmd.dcacheSize  = params->d_cache;
   cmd.options     = params->verbose & 0xff;
   cmd.nCores      = params->ncores;
   cmd.testSpec    = params->tests_config;
   cmd.msg_out     = print_msg;
   if (0 != (params->setup & RUN_AS_APP)) {
      cmd.ioSz = APP_BUFF_SIZE * sizeof(H_UINT);
      if (params->verbose!=0 && 0==(params->setup & RANGE_SPEC))
         params->run_level = 1;
      }
#ifndef NO_DAEMON
   else  {
      poolSize = get_poolsize();
      i = (poolSize + 7)/8 * sizeof(H_UINT);
      cmd.ioSz = sizeof(struct rand_pool_info) + i *sizeof(H_UINT);
      }
#endif
   if (0 != (params->verbose & H_DEBUG_TIME))
      cmd.metering = show_meterInfo;

   if (0 !=(params->setup & CAPTURE) && 0 != (params->run_level == DIAG_RUN_CAPTURE))
      cmd.options |= H_DEBUG_RAW_OUT;
#ifdef  RAW_IN_ENABLE
   if (0 !=(params->setup & INJECT) && 0 != (params->run_level & (DIAG_RUN_INJECT|DIAG_RUN_TEST))) {
      if (strcmp(params->sample_in,"-") == 0 )
        fd_in = stdin;
      else fd_in = fopen(params->sample_in, "rb");
      if (NULL == fd_in)
         error_exit("Unable to open: %s", params->sample_in);
      cmd.injection = injectFile;
      if (params->run_level==DIAG_RUN_INJECT)
         cmd.options |= H_DEBUG_RAW_IN;
      else if (params->run_level==DIAG_RUN_TEST)
         cmd.options |= H_DEBUG_TEST_IN;
      else usage(1, nopts, long_options, cmds);
      }
#endif
   handle = havege_create(&cmd);
   ierr = handle==NULL? H_NOHANDLE : handle->error;
   switch(ierr) {
      case H_NOERR:
         break;
      case H_NOTESTSPEC:
         error_exit("unrecognized test setup: %s", cmd.testSpec);
         break;
      default:
         error_exit("Couldn't initialize haveged (%d)", ierr);
      }
   if (0 != (params->setup & RUN_AS_APP)) {
      if (params->run_level==1)
        anchor_info(handle);
      else if (0==(params->setup&(INJECT|CAPTURE))) {
        /* must specify range with --nunber or --run > 1 but not both */
        if (params->run_level>1) {
          if (0==(params->setup&RANGE_SPEC)) {        /* --run specified    */
            bufct  = params->run_level/sizeof(H_UINT);
            bufrem = (params->run_level%sizeof(H_UINT))*1024;
            }
          else  usage(2, nopts, long_options, cmds);  /* both specified     */
          }
        else if (0==(params->setup&RANGE_SPEC))
          usage(3,nopts, long_options, cmds);        /* neither specified  */
        else if (0==(params->setup&USE_STDOUT)&&(bufct+bufrem)==0)
          usage(4, nopts, long_options, cmds);       /* only with stdout   */
        run_app(handle, bufct, bufrem);
        }
      else if (0==(params->setup&USE_STDOUT)&&(bufct+bufrem)==0)
        usage(5, nopts, long_options, cmds);       /* only with stdout   */
      else run_app(handle, bufct, bufrem);
      }
#ifndef NO_DAEMON
   else run_daemon(handle);
#endif
   havege_destroy(handle);
   exit(0);
}
Ejemplo n.º 2
0
/** @brief Called by the standard allocate buffer, it implements a base functionality.
 *
 * This function can be overriden if the allocation of the buffer is not a simply alloc call.
 * The parameters are the same as the standard function, except for the handle of the port
 * instead of the handler of the component
 * When the buffers needed by this port are all assigned or allocated, the variable
 * bIsFullOfBuffers becomes equal to OMX_TRUE
 */
OMX_ERRORTYPE videoenc_port_AllocateBuffer(
	omx_base_PortType *openmaxStandPort,
	OMX_BUFFERHEADERTYPE **pBuffer,
	OMX_U32 nPortIndex,
	OMX_PTR pAppPrivate,
	OMX_U32 nSizeBytes)
{
	unsigned int i;
	OMX_ERRORTYPE  err = OMX_ErrorNone;
	OMX_COMPONENTTYPE *omxComponent = openmaxStandPort->standCompContainer;
	omx_videoenc_PortType *omx_videoenc_Port = (omx_videoenc_PortType *)openmaxStandPort;
	omx_base_component_PrivateType *omx_base_component_Private = (omx_base_component_PrivateType *)omxComponent->pComponentPrivate;
	OMX_BUFFERHEADERTYPE *pBufferStorage_ACTEXT = NULL;
	OMX_VCE_Buffers_List *pBuffersMng_List= &(omx_videoenc_Port->BuffersMng_List);
	DEBUG(DEB_LEV_FUNCTION_NAME, "In %s for port %p\n", __func__, omx_videoenc_Port);

	if (nPortIndex != omx_videoenc_Port->sPortParam.nPortIndex) {
		return OMX_ErrorBadPortIndex;
	}

	if (PORT_IS_TUNNELED_N_BUFFER_SUPPLIER(omx_videoenc_Port)) {
		return OMX_ErrorBadPortIndex;
	}

	if (omx_base_component_Private->transientState != OMX_TransStateLoadedToIdle) {
		if (!omx_videoenc_Port->bIsTransientToEnabled) {
			DEBUG(DEB_LEV_ERR, "In %s: The port is not allowed to receive buffers\n", __func__);
			return OMX_ErrorIncorrectStateTransition;
		}
	}

	if(nSizeBytes < omx_videoenc_Port->sPortParam.nBufferSize) {
		DEBUG(DEB_LEV_ERR, "In %s: Requested Buffer Size %d is less than Minimum Buffer Size %d\n",
			__func__, nSizeBytes, omx_videoenc_Port->sPortParam.nBufferSize);
		return OMX_ErrorIncorrectStateTransition;
	}

	if(omx_videoenc_Port->ringbuffer == OMX_TRUE && nPortIndex == OMX_BASE_FILTER_OUTPUTPORT_INDEX)
	{
		/* open ringbuffer pool */
		int buffsize = omx_videoenc_Port->ringbuf_framesize * omx_videoenc_Port->sPortParam.nBufferCountActual;
		if(omx_videoenc_Port->bufferpool != NULL)
		{
			if(buffsize > get_poolsize(omx_videoenc_Port->bufferpool))
			{
				pool_dispose(omx_videoenc_Port->bufferpool);
				omx_videoenc_Port->bufferpool = NULL;
				if(pool_open(buffsize, &omx_videoenc_Port->bufferpool))
				{
					DEBUG(DEB_LEV_ERR, "In %s: The port is not allowed to receive buffers %x\n", __func__, buffsize);
					return OMX_ErrorInsufficientResources;
				}
			}
		}
		else
		{
			if( pool_open(buffsize, &omx_videoenc_Port->bufferpool) )
			{
				DEBUG(DEB_LEV_ERR, "In %s: The port is not allowed to receive buffers %x\n", __func__, buffsize);
				return OMX_ErrorInsufficientResources;
			}
		}
	}

	for(i=0; i < omx_videoenc_Port->sPortParam.nBufferCountActual; i++)
	{
		if (omx_videoenc_Port->bBufferStateAllocated[i] == BUFFER_FREE)
		{
			omx_videoenc_Port->pInternalBufferStorage[i] = (OMX_BUFFERHEADERTYPE *)calloc(1, sizeof(OMX_BUFFERHEADERTYPE));
			if (!omx_videoenc_Port->pInternalBufferStorage[i])
			{
				return OMX_ErrorInsufficientResources;
			}

			setHeader(omx_videoenc_Port->pInternalBufferStorage[i], sizeof(OMX_BUFFERHEADERTYPE));

			/* allocate the buffer */
			pBufferStorage_ACTEXT = (OMX_BUFFERHEADERTYPE *)(omx_videoenc_Port->pInternalBufferStorage[i]);
			pBufferStorage_ACTEXT->nAllocLen = nSizeBytes;
			pBufferStorage_ACTEXT->pPlatformPrivate = omx_videoenc_Port;
			pBufferStorage_ACTEXT->pAppPrivate = pAppPrivate;

			if(nPortIndex == OMX_BASE_FILTER_OUTPUTPORT_INDEX &&  omx_videoenc_Port->ringbuffer == OMX_TRUE)
			{
				pBufferStorage_ACTEXT->pBuffer = get_poolbase(omx_videoenc_Port->bufferpool);
				if(pBufferStorage_ACTEXT->pBuffer==NULL)
				{
					DEBUG(DEB_LEV_ERR, "err!get_poolbase is NULL!\n");
					free(pBufferStorage_ACTEXT);
					omx_videoenc_Port->pInternalBufferStorage[i] = NULL;
					return OMX_ErrorInsufficientResources;
				}
			}
			else
			{
				DEBUG(DEB_LEV_PARAMS, "nSizeBytes:%d\n", nSizeBytes);
				err = Add_AllocateBuffer_BuffersMng(pBuffersMng_List, pBufferStorage_ACTEXT, omx_videoenc_Port->bIsStoreMediaData, nSizeBytes);
				if( err != OMX_ErrorNone)
				{
					DEBUG(DEB_LEV_ERR,"err!Add_AllocateBuffer_BuffersMng fail!%s,%d\n", __FILE__, __LINE__);
					free(omx_videoenc_Port->pInternalBufferStorage[i]);
					omx_videoenc_Port->pInternalBufferStorage[i] = NULL;
					return err;
				}
			}

			*pBuffer = (OMX_BUFFERHEADERTYPE *)pBufferStorage_ACTEXT;
			omx_videoenc_Port->bBufferStateAllocated[i] = BUFFER_ALLOCATED;
			omx_videoenc_Port->bBufferStateAllocated[i] |= HEADER_ALLOCATED;
			if (omx_videoenc_Port->sPortParam.eDir == OMX_DirInput)
			{
				pBufferStorage_ACTEXT->nInputPortIndex = omx_videoenc_Port->sPortParam.nPortIndex;
			}
			else
			{
				pBufferStorage_ACTEXT->nOutputPortIndex = omx_videoenc_Port->sPortParam.nPortIndex;
			}
			omx_videoenc_Port->nNumAssignedBuffers++;
			DEBUG(DEB_LEV_PARAMS, "omx_videoenc_Port->nNumAssignedBuffers %i\n", (int)omx_videoenc_Port->nNumAssignedBuffers);

			if (omx_videoenc_Port->sPortParam.nBufferCountActual == omx_videoenc_Port->nNumAssignedBuffers)
			{
				omx_videoenc_Port->sPortParam.bPopulated = OMX_TRUE;
				omx_videoenc_Port->bIsFullOfBuffers = OMX_TRUE;
				DEBUG(DEB_LEV_SIMPLE_SEQ, "In %s nPortIndex=%d\n", __func__, (int)nPortIndex);
				DEBUG(DEB_LEV_PARAMS, "pAllocSem!%s,%d,idx:%d,semval:%d\n", __func__, __LINE__, omx_videoenc_Port->sPortParam.nPortIndex,
					omx_videoenc_Port->pAllocSem->semval);
				tsem_up(omx_videoenc_Port->pAllocSem);
			}
			DEBUG(DEB_LEV_FUNCTION_NAME, "Out of %s for port %p\n", __func__, omx_videoenc_Port);
			return OMX_ErrorNone;
		}
	}

	DEBUG(DEB_LEV_ERR, "Out of %s for port %p. Error: no available buffers\n", __func__, omx_videoenc_Port);
	return OMX_ErrorInsufficientResources;
}