From: Anand Choubey on
Hi All,

I am facing an issue with my driver and user land code. User land code
communicates with driver using overlapped IO mechanism which is
straight forward.
Basic algorithm is at user land:

Thread()
{
Initialize overlapped structure with event = CreateEvent() with
Manual Event Set
issue_again:
Issue IO request using DeviceIOControl()

WaitForMultipleObjects() on overlapped events.

if(event is signaled) then ResetEvent, check result using
GetOverlappedResult(), process data and goto issue_again:
}

Driver side is simple:
just complete IO request.

I do not know exact scenario/reason but sometime GetOverlappedResult
returns 995.

995 definition is:
The I/O operation has been aborted because of either a thread exit or
an application request.

But application and thread both are running. Still i could not get why
OS is throwing the error.
My OS is XP SP3.

Could you please help me to improve OS understanding and cause of
issue?

Regards,
Anand Choubey
From: Tim Roberts on
Anand Choubey <anand.choubey(a)gmail.com> wrote:
>
>I am facing an issue with my driver and user land code. User land code
>communicates with driver using overlapped IO mechanism which is
>straight forward.
>Basic algorithm is at user land:
>
>Thread()
>{
> Initialize overlapped structure with event = CreateEvent() with
>Manual Event Set
>issue_again:
> Issue IO request using DeviceIOControl()
>
> WaitForMultipleObjects() on overlapped events.
>
> if(event is signaled) then ResetEvent, check result using
>GetOverlappedResult(), process data and goto issue_again:
> }
>
>Driver side is simple:
>just complete IO request.
>
>I do not know exact scenario/reason but sometime GetOverlappedResult
>returns 995.

If you don't know the exact scenario, then there's no possible way to
diagnose this. How many requests re you issuing at once? Are there many
threads? How are you constructing the list of handles for
WaitForMultipleObjects? Are you sure you're calling GetOverlappedResult on
the handle that was completed?

Why don't you post the code? We'll see if anything looks unusual.
--
Tim Roberts, timr(a)probo.com
Providenza & Boekelheide, Inc.
From: Anand Choubey on
Hi Tim,

Thanks for your response.
My answers are:

a. How many requests re you issuing at once?
Ans: 64 requested issue at once.

b. Are there many threads?
Ans: Application is having multiple threads but overlapped io is done
by only one thread.

c. How are you constructing the list of handles for
WaitForMultipleObjects?
Ans: There is an array of 64 overlapped events' handle.

d. Are you sure you're calling GetOverlappedResult on the handle that
was completed?
Ans: I think so.

Here is code snip:


DWORD WINAPI DataHandletThread(LPVOID lpParam )
{ //wait for returns fromoverlapped


iCustomBufferSizeInBytes = BUFFER_SIZE;
TOTAL_BUFFERS = 64
for ( i = 0; i < TOTAL_BUFFERS; i++ )
{
hEvent[i] =
overlapped[i].hEvent =
CreateEvent( NULL, TRUE, FALSE, NULL );

if ( overlapped[i].hEvent == NULL )
{
rc = GetLastError();
Log("CreateEvent failed rc=%d\n", rc );
return 0;
}
}


for ( i = 0; i < TOTAL_BUFFERS; i++ )
{

buffer[i] = (PCHAR)malloc(iCustomBufferSizeInBytes);
if ( !DeviceIoControl( hIoctl, IOCTL_GET_DATA, NULL, 0,
buffer[i], iCustomBufferSizeInBytes,
NULL,
&overlapped[i] ) &&
( rc = GetLastError() != ERROR_IO_PENDING ))
{
//Failed here

return 0
}

}

//Creating a circular Queue using WFMO.
/*
* WaitForMultipleObjects returns left mode index of events' array
which is set.
* Example: If events are set in following order: 62,63,0,1 then
* WaitForMultipleObjects will returns 0, 1, 62, 63.
* Therefore 62 and 63 should be read before 0.
*/
int iNextExpected = 0;
bool b1And0Buffer = false;
i = 0;

while (1) {
//
// check if receive buffer is returned
//
rc = WaitForMultipleObjects( TOTAL_BUFFERS, hEvent, FALSE,
INFINITE );

i = rc - WAIT_OBJECT_0;

//This is most bundary condition, when buffer 1 comes before 0.
if((i - iNextExpected) == -1)
{
b1And0Buffer = true;
}


if ( ( i >= 0 ) && ( i < TOTAL_BUFFERS ) )
{
//
// get receive buffer info
//
for(int iIndex = iNextExpected; b1And0Buffer || (iIndex != (i + 1)
% TOTAL_BUFFERS); iIndex = (iIndex + 1) % TOTAL_BUFFERS)
{
b1And0Buffer = false;
ResetEvent(hEvent[iIndex]);

if ( GetOverlappedResult( hIoctl, &overlapped[iIndex],
&bytesReceived,
FALSE ) )
{

if(filterStopping)
break;
bl = (msg_buffer*) buffer[iIndex];

//Data processing here
//with b1


//Reissue the overlapped DIOC request.
if ( !DeviceIoControl( hIoctl, IOCTL_GET_DATA, NULL, 0,
buffer[iIndex], iCustomBufferSizeInBytes,
NULL, &overlapped[iIndex] ) &&
( rc = GetLastError() != ERROR_IO_PENDING ) )
{
//Log error here DIOC failed.
break;
}

}
else
{
if(ERROR_IO_INCOMPLETE != GetLastError())
{
//995 Error occurs.
Log("GetOverlappedResult failed Error %d", GetLastError());
}
break;
}
}
}
else
{
Log("WaitForMultipleObjects failed i = %d rc=%d\n", i, rc);
break;
}

iNextExpected = (i + 1) % TOTAL_BUFFERS;
}


//
// cleanup and exit
//
for ( i = 0; i < TOTAL_BUFFERS; i++ )
{
if ( overlapped[i].hEvent )
{
CloseHandle( overlapped[i].hEvent );
}
}

return 0;
}



Regards,
Anand Choubey

On Mar 22, 9:12 pm, Tim Roberts <t...(a)probo.com> wrote:
> Anand Choubey <anand.chou...(a)gmail.com> wrote:
>
> >I am facing an issue with my driver and user land code. User land code
> >communicates with driver using overlapped IO mechanism which is
> >straight forward.
> >Basic algorithm is at user land:
>
> >Thread()
> >{
> >      Initialize overlapped structure with event = CreateEvent() with
> >Manual Event Set
> >issue_again:
> >      Issue IO request using DeviceIOControl()
>
> >     WaitForMultipleObjects() on overlapped events.
>
> >     if(event is signaled) then ResetEvent, check result using
> >GetOverlappedResult(), process data and goto issue_again:
> >  }
>
> >Driver side is simple:
> >just complete IO request.
>
> >I do not know exact scenario/reason but sometime GetOverlappedResult
> >returns 995.
>
> If you don't know the exact scenario, then there's no possible way to
> diagnose this.  How many requests re you issuing at once?  Are there many
> threads?  How are you constructing the list of handles for
> WaitForMultipleObjects?  Are you sure you're calling GetOverlappedResult on
> the handle that was completed?
>
> Why don't you post the code?  We'll see if anything looks unusual.
> --
> Tim Roberts, t...(a)probo.com
> Providenza & Boekelheide, Inc.

From: Alexander Grigoriev on
1. Your wraparound logic doesn't seem to handle a case when next expected is
0 and i=63.

2. Calling ResetEvent is not necessary. DeviceIoControl will do that.

3. OVERLAPPED should be reinitialized before you call DeviceIoControl with
it again.

4. What driver is that? Is that your custom driver?

"Anand Choubey" <anand.choubey(a)gmail.com> wrote in message
news:6c199537-6f6c-4d68-be69-81a91691585d(a)a10g2000pri.googlegroups.com...
Hi Tim,

Thanks for your response.
My answers are:

a. How many requests re you issuing at once?
Ans: 64 requested issue at once.

b. Are there many threads?
Ans: Application is having multiple threads but overlapped io is done
by only one thread.

c. How are you constructing the list of handles for
WaitForMultipleObjects?
Ans: There is an array of 64 overlapped events' handle.

d. Are you sure you're calling GetOverlappedResult on the handle that
was completed?
Ans: I think so.

Here is code snip:


DWORD WINAPI DataHandletThread(LPVOID lpParam )
{ //wait for returns fromoverlapped


iCustomBufferSizeInBytes = BUFFER_SIZE;
TOTAL_BUFFERS = 64
for ( i = 0; i < TOTAL_BUFFERS; i++ )
{
hEvent[i] =
overlapped[i].hEvent =
CreateEvent( NULL, TRUE, FALSE, NULL );

if ( overlapped[i].hEvent == NULL )
{
rc = GetLastError();
Log("CreateEvent failed rc=%d\n", rc );
return 0;
}
}


for ( i = 0; i < TOTAL_BUFFERS; i++ )
{

buffer[i] = (PCHAR)malloc(iCustomBufferSizeInBytes);
if ( !DeviceIoControl( hIoctl, IOCTL_GET_DATA, NULL, 0,
buffer[i], iCustomBufferSizeInBytes,
NULL,
&overlapped[i] ) &&
( rc = GetLastError() != ERROR_IO_PENDING ))
{
//Failed here

return 0
}

}

//Creating a circular Queue using WFMO.
/*
* WaitForMultipleObjects returns left mode index of events' array
which is set.
* Example: If events are set in following order: 62,63,0,1 then
* WaitForMultipleObjects will returns 0, 1, 62, 63.
* Therefore 62 and 63 should be read before 0.
*/
int iNextExpected = 0;
bool b1And0Buffer = false;
i = 0;

while (1) {
//
// check if receive buffer is returned
//
rc = WaitForMultipleObjects( TOTAL_BUFFERS, hEvent, FALSE,
INFINITE );

i = rc - WAIT_OBJECT_0;

//This is most bundary condition, when buffer 1 comes before 0.
if((i - iNextExpected) == -1)
{
b1And0Buffer = true;
}


if ( ( i >= 0 ) && ( i < TOTAL_BUFFERS ) )
{
//
// get receive buffer info
//
for(int iIndex = iNextExpected; b1And0Buffer || (iIndex != (i + 1)
% TOTAL_BUFFERS); iIndex = (iIndex + 1) % TOTAL_BUFFERS)
{
b1And0Buffer = false;
ResetEvent(hEvent[iIndex]);

if ( GetOverlappedResult( hIoctl, &overlapped[iIndex],
&bytesReceived,
FALSE ) )
{

if(filterStopping)
break;
bl = (msg_buffer*) buffer[iIndex];

//Data processing here
//with b1


//Reissue the overlapped DIOC request.
if ( !DeviceIoControl( hIoctl, IOCTL_GET_DATA, NULL, 0,
buffer[iIndex], iCustomBufferSizeInBytes,
NULL, &overlapped[iIndex] ) &&
( rc = GetLastError() != ERROR_IO_PENDING ) )
{
//Log error here DIOC failed.
break;
}

}
else
{
if(ERROR_IO_INCOMPLETE != GetLastError())
{
//995 Error occurs.
Log("GetOverlappedResult failed Error %d", GetLastError());
}
break;
}
}
}
else
{
Log("WaitForMultipleObjects failed i = %d rc=%d\n", i, rc);
break;
}

iNextExpected = (i + 1) % TOTAL_BUFFERS;
}


//
// cleanup and exit
//
for ( i = 0; i < TOTAL_BUFFERS; i++ )
{
if ( overlapped[i].hEvent )
{
CloseHandle( overlapped[i].hEvent );
}
}

return 0;
}



Regards,
Anand Choubey

On Mar 22, 9:12 pm, Tim Roberts <t...(a)probo.com> wrote:
> Anand Choubey <anand.chou...(a)gmail.com> wrote:
>
> >I am facing an issue with my driver and user land code. User land code
> >communicates with driver using overlapped IO mechanism which is
> >straight forward.
> >Basic algorithm is at user land:
>
> >Thread()
> >{
> > Initialize overlapped structure with event = CreateEvent() with
> >Manual Event Set
> >issue_again:
> > Issue IO request using DeviceIOControl()
>
> > WaitForMultipleObjects() on overlapped events.
>
> > if(event is signaled) then ResetEvent, check result using
> >GetOverlappedResult(), process data and goto issue_again:
> > }
>
> >Driver side is simple:
> >just complete IO request.
>
> >I do not know exact scenario/reason but sometime GetOverlappedResult
> >returns 995.
>
> If you don't know the exact scenario, then there's no possible way to
> diagnose this. How many requests re you issuing at once? Are there many
> threads? How are you constructing the list of handles for
> WaitForMultipleObjects? Are you sure you're calling GetOverlappedResult on
> the handle that was completed?
>
> Why don't you post the code? We'll see if anything looks unusual.
> --
> Tim Roberts, t...(a)probo.com
> Providenza & Boekelheide, Inc.


From: Anand Choubey on
Hi,

Thanks for reply.

a. What driver is that? Is that your custom driver?
Ans: It is custom driver.

b. 3. OVERLAPPED should be reinitialized before you call
DeviceIoControl with
it again.
Ans: I have doubt on it:
Should I create new event object i.e. CreateEvent or just
reinitialized OVERLAPPED structure field except Event field?

c. My issue is GetOverlappedResult returns 995 i.e. Thread/Application
aborted. But neither thread is aborted nor application terminated.
What would be potential reason of above error?

Could you please let me the answer of above queries?

Thanks again.

Regards,

Anand Choubey

On Mar 23, 3:53 pm, "Alexander Grigoriev" <al...(a)earthlink.net> wrote:
> 1. Your wraparound logic doesn't seem to handle a case when next expected is
> 0 and i=63.
>
> 2. Calling ResetEvent is not necessary. DeviceIoControl will do that.
>
> 3. OVERLAPPED should be reinitialized before you call DeviceIoControl with
> it again.
>
> 4. What driver is that? Is that your custom driver?
>
> "Anand Choubey" <anand.chou...(a)gmail.com> wrote in message
>
> news:6c199537-6f6c-4d68-be69-81a91691585d(a)a10g2000pri.googlegroups.com...
> Hi Tim,
>
> Thanks for your response.
> My answers are:
>
> a. How many requests re you issuing at once?
> Ans: 64 requested issue at once.
>
> b. Are there many threads?
> Ans: Application is having multiple threads but overlapped io is done
> by only one thread.
>
> c. How are you constructing the list of handles for
> WaitForMultipleObjects?
> Ans: There is an array of 64 overlapped events' handle.
>
> d. Are you sure you're calling GetOverlappedResult on the handle that
> was completed?
> Ans:  I think so.
>
> Here is code snip:
>
> DWORD WINAPI DataHandletThread(LPVOID lpParam )
> {   //wait for returns fromoverlapped
>
>     iCustomBufferSizeInBytes = BUFFER_SIZE;
>     TOTAL_BUFFERS = 64
>     for ( i = 0; i < TOTAL_BUFFERS; i++ )
>     {
>        hEvent[i] =
>             overlapped[i].hEvent =
>             CreateEvent( NULL, TRUE, FALSE, NULL );
>
>         if ( overlapped[i].hEvent == NULL )
>         {
>             rc = GetLastError();
>             Log("CreateEvent failed rc=%d\n", rc );
>             return 0;
>         }
>     }
>
>     for ( i = 0; i < TOTAL_BUFFERS; i++ )
>     {
>
> buffer[i] = (PCHAR)malloc(iCustomBufferSizeInBytes);
> if ( !DeviceIoControl( hIoctl, IOCTL_GET_DATA, NULL, 0,
> buffer[i], iCustomBufferSizeInBytes,
> NULL,
> &overlapped[i] ) &&
> ( rc = GetLastError() != ERROR_IO_PENDING ))
> {
> //Failed here
>
> return 0
>
> }
>
>     }
>
> //Creating a circular Queue using WFMO.
> /*
> * WaitForMultipleObjects returns left mode index of events' array
> which is set.
> * Example: If events are set in following order: 62,63,0,1 then
> * WaitForMultipleObjects will returns 0, 1, 62, 63.
> * Therefore 62 and 63 should be read before 0.
> */
> int iNextExpected = 0;
> bool b1And0Buffer = false;
> i = 0;
>
>     while (1) {
>         //
>         // check if receive buffer is returned
>         //
>         rc = WaitForMultipleObjects( TOTAL_BUFFERS, hEvent, FALSE,
> INFINITE );
>
>         i = rc - WAIT_OBJECT_0;
>
> //This is most bundary condition, when buffer 1 comes before 0.
> if((i - iNextExpected) == -1)
> {
> b1And0Buffer = true;
>
> }
>
>         if ( ( i >= 0 ) && ( i < TOTAL_BUFFERS ) )
>         {
>             //
>             // get receive buffer info
>             //
> for(int iIndex = iNextExpected; b1And0Buffer || (iIndex != (i + 1)
> % TOTAL_BUFFERS); iIndex = (iIndex + 1) % TOTAL_BUFFERS)
> {
> b1And0Buffer = false;
> ResetEvent(hEvent[iIndex]);
>
> if ( GetOverlappedResult( hIoctl, &overlapped[iIndex],
> &bytesReceived,
> FALSE ) )
> {
>
> if(filterStopping)
> break;
> bl = (msg_buffer*) buffer[iIndex];
>
> //Data processing here
> //with b1
>
> //Reissue the overlapped DIOC request.
> if ( !DeviceIoControl( hIoctl, IOCTL_GET_DATA, NULL, 0,
> buffer[iIndex], iCustomBufferSizeInBytes,
> NULL, &overlapped[iIndex] ) &&
> ( rc = GetLastError() != ERROR_IO_PENDING ) )
> {
> //Log error here DIOC failed.
> break;
>
> }
> }
>
> else
> {
> if(ERROR_IO_INCOMPLETE != GetLastError())
> {
> //995 Error occurs.
> Log("GetOverlappedResult failed Error %d", GetLastError());}
> break;
> }
> }
>
>         }
>         else
> {
> Log("WaitForMultipleObjects failed i = %d rc=%d\n", i, rc);
> break;
>
> }
>
> iNextExpected = (i + 1) % TOTAL_BUFFERS;
>     }
>
>     //
>     // cleanup and exit
>     //
>     for ( i = 0; i < TOTAL_BUFFERS; i++ )
>     {
>         if ( overlapped[i].hEvent )
>         {
>             CloseHandle( overlapped[i].hEvent );
>         }
>     }
>
>     return 0;
>
> }
>
> Regards,
> Anand Choubey
>
> On Mar 22, 9:12 pm, Tim Roberts <t...(a)probo.com> wrote:
>
>
>
> > Anand Choubey <anand.chou...(a)gmail.com> wrote:
>
> > >I am facing an issue with my driver and user land code. User land code
> > >communicates with driver using overlapped IO mechanism which is
> > >straight forward.
> > >Basic algorithm is at user land:
>
> > >Thread()
> > >{
> > > Initialize overlapped structure with event = CreateEvent() with
> > >Manual Event Set
> > >issue_again:
> > > Issue IO request using DeviceIOControl()
>
> > > WaitForMultipleObjects() on overlapped events.
>
> > > if(event is signaled) then ResetEvent, check result using
> > >GetOverlappedResult(), process data and goto issue_again:
> > > }
>
> > >Driver side is simple:
> > >just complete IO request.
>
> > >I do not know exact scenario/reason but sometime GetOverlappedResult
> > >returns 995.
>
> > If you don't know the exact scenario, then there's no possible way to
> > diagnose this. How many requests re you issuing at once? Are there many
> > threads? How are you constructing the list of handles for
> > WaitForMultipleObjects? Are you sure you're calling GetOverlappedResult on
> > the handle that was completed?
>
> > Why don't you post the code? We'll see if anything looks unusual.
> > --
> > Tim Roberts, t...(a)probo.com
> > Providenza & Boekelheide, Inc.